|
1
|
|
|
/* bignumber.js v1.0.1 https://github.com/MikeMcl/bignumber.js/LICENCE */ |
|
2
|
|
|
;(function ( global ) { |
|
3
|
|
|
'use strict'; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
bignumber-1.0.1.js v1.0.1 |
|
7
|
|
|
A Javascript library for arbitrary-precision arithmetic. |
|
8
|
|
|
https://github.com/MikeMcl/bignumber-1.0.1.js |
|
9
|
|
|
Copyright (c) 2012 Michael Mclaughlin <[email protected]> |
|
10
|
|
|
MIT Expat Licence |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/*********************************** DEFAULTS ************************************/ |
|
14
|
|
|
|
|
15
|
|
|
/* |
|
16
|
|
|
* The default values below must be integers within the stated ranges (inclusive). |
|
17
|
|
|
* Most of these values can be changed programmatically using BigNumber.config(). |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/* |
|
21
|
|
|
* The limit on the value of DECIMAL_PLACES, TO_EXP_NEG, TO_EXP_POS, MIN_EXP, |
|
22
|
|
|
* MAX_EXP, and the argument to toFixed, toPrecision and toExponential, beyond |
|
23
|
|
|
* which an exception is thrown (if ERRORS is true). |
|
24
|
|
|
*/ |
|
25
|
|
|
var MAX = 1E9, // 0 to 1e+9 |
|
26
|
|
|
|
|
27
|
|
|
// Limit of magnitude of exponent argument to toPower. |
|
28
|
|
|
MAX_POWER = 1E6, // 1 to 1e+6 |
|
29
|
|
|
|
|
30
|
|
|
// The maximum number of decimal places for operations involving division. |
|
31
|
|
|
DECIMAL_PLACES = 20, // 0 to MAX |
|
32
|
|
|
|
|
33
|
|
|
/* |
|
34
|
|
|
* The rounding mode used when rounding to the above decimal places, and when |
|
35
|
|
|
* using toFixed, toPrecision and toExponential, and round (default value). |
|
36
|
|
|
* UP 0 Away from zero. |
|
37
|
|
|
* DOWN 1 Towards zero. |
|
38
|
|
|
* CEIL 2 Towards +Infinity. |
|
39
|
|
|
* FLOOR 3 Towards -Infinity. |
|
40
|
|
|
* HALF_UP 4 Towards nearest neighbour. If equidistant, up. |
|
41
|
|
|
* HALF_DOWN 5 Towards nearest neighbour. If equidistant, down. |
|
42
|
|
|
* HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour. |
|
43
|
|
|
* HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity. |
|
44
|
|
|
* HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity. |
|
45
|
|
|
*/ |
|
46
|
|
|
ROUNDING_MODE = 4, // 0 to 8 |
|
47
|
|
|
|
|
48
|
|
|
// EXPONENTIAL_AT : [TO_EXP_NEG , TO_EXP_POS] |
|
49
|
|
|
|
|
50
|
|
|
// The exponent value at and beneath which toString returns exponential notation. |
|
51
|
|
|
// Number type: -7 |
|
52
|
|
|
TO_EXP_NEG = -7, // 0 to -MAX |
|
53
|
|
|
|
|
54
|
|
|
// The exponent value at and above which toString returns exponential notation. |
|
55
|
|
|
// Number type: 21 |
|
56
|
|
|
TO_EXP_POS = 21, // 0 to MAX |
|
57
|
|
|
|
|
58
|
|
|
// RANGE : [MIN_EXP, MAX_EXP] |
|
59
|
|
|
|
|
60
|
|
|
// The minimum exponent value, beneath which underflow to zero occurs. |
|
61
|
|
|
// Number type: -324 (5e-324) |
|
62
|
|
|
MIN_EXP = -MAX, // -1 to -MAX |
|
63
|
|
|
|
|
64
|
|
|
// The maximum exponent value, above which overflow to Infinity occurs. |
|
65
|
|
|
// Number type: 308 (1.7976931348623157e+308) |
|
66
|
|
|
MAX_EXP = MAX, // 1 to MAX |
|
67
|
|
|
|
|
68
|
|
|
// Whether BigNumber Errors are ever thrown. |
|
69
|
|
|
// CHANGE parseInt to parseFloat if changing ERRORS to false. |
|
70
|
|
|
ERRORS = true, // true or false |
|
71
|
|
|
parse = parseInt, // parseInt or parseFloat |
|
72
|
|
|
|
|
73
|
|
|
/***********************************************************************************/ |
|
74
|
|
|
|
|
75
|
|
|
P = BigNumber.prototype, |
|
76
|
|
|
DIGITS = '0123456789abcdefghijklmnopqrstuvwxyz', |
|
77
|
|
|
outOfRange, |
|
78
|
|
|
id = 0, |
|
79
|
|
|
isValid = /^-?\d+(?:\.\d+)?(?:e[+-]?\d+)?$/i, |
|
80
|
|
|
trim = String.prototype.trim || function () {return this.replace(/^\s+|\s+$/g, '')}, |
|
81
|
|
|
ONE = BigNumber(1); |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
// CONSTRUCTOR |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/* |
|
88
|
|
|
* The exported function. |
|
89
|
|
|
* Create and return a new instance of a BigNumber object. |
|
90
|
|
|
* |
|
91
|
|
|
* n {number|string|BigNumber} A numeric value. |
|
92
|
|
|
* [b] {number} The base of n. Integer, 2 to 36 inclusive. |
|
93
|
|
|
*/ |
|
94
|
|
|
function BigNumber( n, b ) { |
|
95
|
|
|
var isNum, i, j, |
|
96
|
|
|
x = this; |
|
97
|
|
|
|
|
98
|
|
|
// Enable constructor usage without new. |
|
99
|
|
|
if ( !(x instanceof BigNumber) ) { |
|
100
|
|
|
return new BigNumber( n, b ) |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Duplicate. |
|
104
|
|
|
if ( n instanceof BigNumber ) { |
|
105
|
|
|
id = 0; |
|
106
|
|
|
|
|
107
|
|
|
// i is undefined. |
|
108
|
|
|
if ( b !== i) { |
|
109
|
|
|
n = n['toS']() |
|
110
|
|
|
} else { |
|
111
|
|
|
x['s'] = n['s']; |
|
112
|
|
|
x['e'] = n['e']; |
|
113
|
|
|
x['c'] = ( n = n['c'] ) ? n.slice() : n; |
|
114
|
|
|
return |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// Check if number and if minus zero. Convert to string. |
|
119
|
|
|
if ( typeof n != 'string' ) { |
|
120
|
|
|
n = ( isNum = Object.prototype.toString.call(n) == '[object Number]' ) && |
|
121
|
|
|
n === 0 && 1 / n < 0 ? '-0' : n + '' |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if ( b === i && isValid.test(n) ) { |
|
125
|
|
|
|
|
126
|
|
|
// Determine sign. |
|
127
|
|
|
x['s'] = n.charAt(0) == '-' ? ( n = n.slice(1), -1 ) : 1 |
|
128
|
|
|
|
|
129
|
|
|
// Either n is not a valid BigNumber or a base has been specified. |
|
130
|
|
|
} else { |
|
131
|
|
|
|
|
132
|
|
|
// Enable exponential notation to be used with base 10 argument. |
|
133
|
|
|
// Ensure return value is rounded to DECIMAL_PLACES as with other bases. |
|
134
|
|
|
if ( b == 10 ) { |
|
135
|
|
|
return new BigNumber(n)['div'](ONE) |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/* |
|
139
|
|
|
* Follow Javascript numbers in allowing numbers with fraction digits |
|
140
|
|
|
* to omit a leading zero and allowing a leading plus sign e.g. '+.5' for '0.5'. |
|
141
|
|
|
*/ |
|
142
|
|
|
n = trim.call(n).replace( /^\+(?!-)/, '' ).replace( /^(-?)\./, '$10.' ); |
|
143
|
|
|
|
|
144
|
|
|
x['s'] = n.charAt(0) == '-' ? ( n = n.replace( /^-(?!-)/, '' ), -1 ) : 1; |
|
145
|
|
|
|
|
146
|
|
|
if ( b != null ) { |
|
147
|
|
|
|
|
148
|
|
|
if ( ( b == (b | 0) || !ERRORS ) && |
|
149
|
|
|
!( outOfRange = !( b >= 2 && b <= 36 ) ) ) { |
|
150
|
|
|
|
|
151
|
|
|
i = '[' + DIGITS.slice( 0, b = b | 0 ) + ']+'; |
|
152
|
|
|
|
|
153
|
|
|
// Test non-decimal number validity. |
|
154
|
|
|
// Any number in exponential form will fail due to the e+/-. |
|
155
|
|
|
if ( j = new RegExp( '^' + i + '(?:\\.' + i + ')?$', 'i' ).test(n) ) { |
|
156
|
|
|
|
|
157
|
|
|
if ( isNum ) { |
|
158
|
|
|
if ( n.replace('.', '').length > 15 ) { |
|
159
|
|
|
|
|
160
|
|
|
// 'new BigNumber() number type has more than 15 significant digits: {n}' |
|
161
|
|
|
ifExceptionsThrow( n, 0 ) |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
// Prevent later check for length on converted number. |
|
165
|
|
|
isNum = !isNum |
|
166
|
|
|
} |
|
167
|
|
|
n = convert( n, 10, b, x['s'] ) |
|
168
|
|
|
} else if ( n != 'Infinity' && n != 'NaN' ) { |
|
169
|
|
|
|
|
170
|
|
|
// 'new BigNumber() not a base {b} number: {n}' |
|
171
|
|
|
ifExceptionsThrow( n, 1, b ); |
|
172
|
|
|
n = 'NaN' |
|
173
|
|
|
} |
|
174
|
|
|
} else { |
|
175
|
|
|
|
|
176
|
|
|
// 'new BigNumber() base not an integer: {b}' |
|
177
|
|
|
// 'new BigNumber() base out of range: {b}' |
|
178
|
|
|
ifExceptionsThrow( b, 2 ); |
|
179
|
|
|
|
|
180
|
|
|
// Ignore base. |
|
181
|
|
|
j = isValid.test(n) |
|
182
|
|
|
} |
|
183
|
|
|
} else { |
|
184
|
|
|
j = isValid.test(n) |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
if ( !j ) { |
|
188
|
|
|
|
|
189
|
|
|
// Infinity/NaN |
|
190
|
|
|
x['c'] = x['e'] = null; |
|
191
|
|
|
|
|
192
|
|
|
// NaN |
|
193
|
|
|
if ( n != 'Infinity' ) { |
|
194
|
|
|
|
|
195
|
|
|
// No exception on NaN. |
|
196
|
|
|
if ( n != 'NaN' ) { |
|
197
|
|
|
|
|
198
|
|
|
// 'new BigNumber() not a number: {n}' |
|
199
|
|
|
ifExceptionsThrow( n, 3 ) |
|
200
|
|
|
} |
|
201
|
|
|
x['s'] = null |
|
202
|
|
|
} |
|
203
|
|
|
id = 0; |
|
204
|
|
|
|
|
205
|
|
|
return |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
// Decimal point? |
|
210
|
|
|
if ( ( i = n.indexOf('.') ) > -1 ) { |
|
211
|
|
|
n = n.replace( '.', '' ) |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
// Exponential form? |
|
215
|
|
|
if ( ( j = n.search(/e/i) ) > 0 ) { |
|
216
|
|
|
|
|
217
|
|
|
// Determine exponent. |
|
218
|
|
|
if ( i < 0 ) { |
|
219
|
|
|
i = j |
|
220
|
|
|
} |
|
221
|
|
|
i += +n.slice( j + 1 ); |
|
222
|
|
|
n = n.substring( 0, j ) |
|
223
|
|
|
|
|
224
|
|
|
} else if ( i < 0 ) { |
|
225
|
|
|
|
|
226
|
|
|
// Integer. |
|
227
|
|
|
i = n.length |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
// Disallow numbers over 15 digits if number type. |
|
231
|
|
|
if ( b = n.length, isNum && b > 15 ) { |
|
232
|
|
|
|
|
233
|
|
|
// 'new BigNumber() number type has more than 15 significant digits: {n}' |
|
234
|
|
|
ifExceptionsThrow( n, 0 ) |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
// Determine leading zeros. |
|
238
|
|
|
for ( id = j = 0; n.charAt(j) == '0'; j++ ) { |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
// Overflow? |
|
242
|
|
|
if ( ( i -= j + 1 ) > MAX_EXP ) { |
|
243
|
|
|
|
|
244
|
|
|
// Infinity. |
|
245
|
|
|
x['c'] = x['e'] = null |
|
246
|
|
|
|
|
247
|
|
|
// Zero or underflow? |
|
248
|
|
|
} else if ( j == b || i < MIN_EXP ) { |
|
249
|
|
|
|
|
250
|
|
|
// Zero. |
|
251
|
|
|
x['c'] = [ x['e'] = 0 ] |
|
252
|
|
|
} else { |
|
253
|
|
|
|
|
254
|
|
|
// Determine trailing zeros. |
|
255
|
|
|
for ( ; n.charAt(--b) == '0'; ) { |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
x['e'] = i; |
|
259
|
|
|
x['c'] = []; |
|
260
|
|
|
|
|
261
|
|
|
// Convert string to array of digits (without leading and trailing zeros). |
|
262
|
|
|
for ( i = 0; j <= b; x['c'][i++] = +n.charAt(j++) ) { |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
|
|
268
|
|
|
// CONSTRUCTOR PROPERTIES/METHODS |
|
269
|
|
|
|
|
270
|
|
|
|
|
271
|
|
|
BigNumber['ROUND_UP'] = 0; |
|
272
|
|
|
BigNumber['ROUND_DOWN'] = 1; |
|
273
|
|
|
BigNumber['ROUND_CEIL'] = 2; |
|
274
|
|
|
BigNumber['ROUND_FLOOR'] = 3; |
|
275
|
|
|
BigNumber['ROUND_HALF_UP'] = 4; |
|
276
|
|
|
BigNumber['ROUND_HALF_DOWN'] = 5; |
|
277
|
|
|
BigNumber['ROUND_HALF_EVEN'] = 6; |
|
278
|
|
|
BigNumber['ROUND_HALF_CEIL'] = 7; |
|
279
|
|
|
BigNumber['ROUND_HALF_FLOOR'] = 8; |
|
280
|
|
|
|
|
281
|
|
|
|
|
282
|
|
|
/* |
|
283
|
|
|
* Configure infrequently-changing library-wide settings. |
|
284
|
|
|
* |
|
285
|
|
|
* Accept an object or an argument list, with one or many of the following |
|
286
|
|
|
* properties or parameters respectively: |
|
287
|
|
|
* [ DECIMAL_PLACES [, ROUNDING_MODE [, EXPONENTIAL_AT [, RANGE [, ERRORS ]]]]] |
|
288
|
|
|
* |
|
289
|
|
|
* E.g. |
|
290
|
|
|
* BigNumber.config(20, 4) is equivalent to |
|
291
|
|
|
* BigNumber.config({ DECIMAL_PLACES : 20, ROUNDING_MODE : 4 }) |
|
292
|
|
|
* Ignore properties/parameters set to null or undefined. |
|
293
|
|
|
* |
|
294
|
|
|
* Return an object with the properties current values. |
|
295
|
|
|
*/ |
|
296
|
|
|
BigNumber['config'] = function () { |
|
297
|
|
|
var v, p, |
|
298
|
|
|
i = 0, |
|
299
|
|
|
r = {}, |
|
300
|
|
|
a = arguments, |
|
301
|
|
|
o = a[0], |
|
302
|
|
|
c = 'config', |
|
303
|
|
|
inRange = function ( n, lo, hi ) { |
|
304
|
|
|
return !( ( outOfRange = n < lo || n > hi ) || |
|
305
|
|
|
parse(n) != n && n !== 0 ) |
|
306
|
|
|
}, |
|
307
|
|
|
has = o && typeof o == 'object' |
|
308
|
|
|
? function () {if ( o.hasOwnProperty(p) ) return ( v = o[p] ) != null} |
|
309
|
|
|
: function () {if ( a.length > i ) return ( v = a[i++] ) != null}; |
|
310
|
|
|
|
|
311
|
|
|
// [DECIMAL_PLACES] {number} Integer, 0 to MAX inclusive. |
|
312
|
|
|
if ( has( p = 'DECIMAL_PLACES' ) ) { |
|
313
|
|
|
|
|
314
|
|
|
if ( inRange( v, 0, MAX ) ) { |
|
315
|
|
|
DECIMAL_PLACES = v | 0 |
|
316
|
|
|
} else { |
|
317
|
|
|
|
|
318
|
|
|
// 'config() DECIMAL_PLACES not an integer: {v}' |
|
319
|
|
|
// 'config() DECIMAL_PLACES out of range: {v}' |
|
320
|
|
|
ifExceptionsThrow( v, p, c ) |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
r[p] = DECIMAL_PLACES; |
|
324
|
|
|
|
|
325
|
|
|
// [ROUNDING_MODE] {number} Integer, 0 to 8 inclusive. |
|
326
|
|
|
if ( has( p = 'ROUNDING_MODE' ) ) { |
|
327
|
|
|
|
|
328
|
|
|
if ( inRange( v, 0, 8 ) ) { |
|
329
|
|
|
ROUNDING_MODE = v | 0 |
|
330
|
|
|
} else { |
|
331
|
|
|
|
|
332
|
|
|
// 'config() ROUNDING_MODE not an integer: {v}' |
|
333
|
|
|
// 'config() ROUNDING_MODE out of range: {v}' |
|
334
|
|
|
ifExceptionsThrow( v, p, c ) |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
r[p] = ROUNDING_MODE; |
|
338
|
|
|
|
|
339
|
|
|
/* |
|
340
|
|
|
* [EXPONENTIAL_AT] {number|number[]} Integer, -MAX to MAX inclusive or |
|
341
|
|
|
* [ integer -MAX to 0 inclusive, 0 to MAX inclusive ]. |
|
342
|
|
|
*/ |
|
343
|
|
|
if ( has( p = 'EXPONENTIAL_AT' ) ) { |
|
344
|
|
|
|
|
345
|
|
|
if ( inRange( v, -MAX, MAX ) ) { |
|
346
|
|
|
TO_EXP_NEG = -( TO_EXP_POS = ~~( v < 0 ? -v : +v ) ) |
|
347
|
|
|
} else if ( !outOfRange && v && inRange( v[0], -MAX, 0 ) && |
|
348
|
|
|
inRange( v[1], 0, MAX ) ) { |
|
349
|
|
|
TO_EXP_NEG = ~~v[0], TO_EXP_POS = ~~v[1] |
|
350
|
|
|
} else { |
|
351
|
|
|
|
|
352
|
|
|
// 'config() EXPONENTIAL_AT not an integer or not [integer, integer]: {v}' |
|
353
|
|
|
// 'config() EXPONENTIAL_AT out of range or not [negative, positive: {v}' |
|
354
|
|
|
ifExceptionsThrow( v, p, c, 1 ) |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
r[p] = [ TO_EXP_NEG, TO_EXP_POS ]; |
|
358
|
|
|
|
|
359
|
|
|
/* |
|
360
|
|
|
* [RANGE][ {number|number[]} Non-zero integer, -MAX to MAX inclusive or |
|
361
|
|
|
* [ integer -MAX to -1 inclusive, integer 1 to MAX inclusive ]. |
|
362
|
|
|
*/ |
|
363
|
|
|
if ( has( p = 'RANGE' ) ) { |
|
364
|
|
|
|
|
365
|
|
|
if ( inRange( v, -MAX, MAX ) && ~~v ) { |
|
366
|
|
|
MIN_EXP = -( MAX_EXP = ~~( v < 0 ? -v : +v ) ) |
|
367
|
|
|
} else if ( !outOfRange && v && inRange( v[0], -MAX, -1 ) && |
|
368
|
|
|
inRange( v[1], 1, MAX ) ) { |
|
369
|
|
|
MIN_EXP = ~~v[0], MAX_EXP = ~~v[1] |
|
370
|
|
|
} else { |
|
371
|
|
|
|
|
372
|
|
|
// 'config() RANGE not a non-zero integer or not [integer, integer]: {v}' |
|
373
|
|
|
// 'config() RANGE out of range or not [negative, positive: {v}' |
|
374
|
|
|
ifExceptionsThrow( v, p, c, 1, 1 ) |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
r[p] = [ MIN_EXP, MAX_EXP ]; |
|
378
|
|
|
|
|
379
|
|
|
// [ERRORS] {boolean|number} true, false, 1 or 0. |
|
380
|
|
|
if ( has( p = 'ERRORS' ) ) { |
|
381
|
|
|
|
|
382
|
|
|
if ( v === !!v || v === 1 || v === 0 ) { |
|
383
|
|
|
parse = ( outOfRange = id = 0, ERRORS = !!v ) |
|
384
|
|
|
? parseInt |
|
385
|
|
|
: parseFloat |
|
386
|
|
|
} else { |
|
387
|
|
|
|
|
388
|
|
|
// 'config() ERRORS not a boolean or binary digit: {v}' |
|
389
|
|
|
ifExceptionsThrow( v, p, c, 0, 0, 1 ) |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
r[p] = ERRORS; |
|
393
|
|
|
|
|
394
|
|
|
return r |
|
395
|
|
|
}; |
|
396
|
|
|
|
|
397
|
|
|
|
|
398
|
|
|
// PRIVATE FUNCTIONS |
|
399
|
|
|
|
|
400
|
|
|
|
|
401
|
|
|
// Assemble error messages. Throw BigNumber Errors. |
|
402
|
|
|
function ifExceptionsThrow( arg, i, j, isArray, isRange, isErrors) { |
|
403
|
|
|
if ( ERRORS ) { |
|
404
|
|
|
var method = ['new BigNumber', 'cmp', 'div', 'eq', 'gt', 'gte', 'lt', |
|
405
|
|
|
'lte', 'minus', 'mod', 'plus', 'times', 'toFr' |
|
406
|
|
|
][ id ? id < 0 ? -id : id : 1 / id < 0 ? 1 : 0 ] + '()', |
|
407
|
|
|
error = outOfRange ? ' out of range' : ' not a' + |
|
408
|
|
|
( isRange ? ' non-zero' : 'n' ) + ' integer'; |
|
409
|
|
|
|
|
410
|
|
|
error = ( [ |
|
411
|
|
|
method + ' number type has more than 15 significant digits', |
|
412
|
|
|
method + ' not a base ' + j + ' number', |
|
413
|
|
|
method + ' base' + error, |
|
414
|
|
|
method + ' not a number' ][i] || |
|
415
|
|
|
j + '() ' + i + ( isErrors |
|
416
|
|
|
? ' not a boolean or binary digit' |
|
417
|
|
|
: error + ( isArray |
|
418
|
|
|
? ' or not [' + ( outOfRange |
|
419
|
|
|
? ' negative, positive' |
|
420
|
|
|
: ' integer, integer' ) + ' ]' |
|
421
|
|
|
: '' ) ) ) + ': ' + arg; |
|
422
|
|
|
|
|
423
|
|
|
outOfRange = id = 0; |
|
424
|
|
|
throw { |
|
425
|
|
|
name : 'BigNumber Error', |
|
426
|
|
|
message : error, |
|
427
|
|
|
toString : function () {return this.name + ': ' + this.message} |
|
428
|
|
|
} |
|
429
|
|
|
} |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
|
|
433
|
|
|
/* |
|
434
|
|
|
* Convert a numeric string of baseIn to a numeric string of baseOut. |
|
435
|
|
|
*/ |
|
436
|
|
|
function convert( nStr, baseOut, baseIn, sign ) { |
|
437
|
|
|
var e, dvs, dvd, nArr, fracArr, fracBN; |
|
438
|
|
|
|
|
439
|
|
|
// Convert string of base bIn to an array of numbers of baseOut. |
|
440
|
|
|
// Eg. strToArr('255', 10) where baseOut is 16, returns [15, 15]. |
|
441
|
|
|
// Eg. strToArr('ff', 16) where baseOut is 10, returns [2, 5, 5]. |
|
442
|
|
|
function strToArr( str, bIn ) { |
|
443
|
|
|
var j, |
|
444
|
|
|
i = 0, |
|
445
|
|
|
strL = str.length, |
|
446
|
|
|
arrL, |
|
447
|
|
|
arr = [0]; |
|
448
|
|
|
|
|
449
|
|
|
for ( bIn = bIn || baseIn; i < strL; i++ ) { |
|
450
|
|
|
|
|
451
|
|
|
for ( arrL = arr.length, j = 0; j < arrL; arr[j] *= bIn, j++ ) { |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
for ( arr[0] += DIGITS.indexOf( str.charAt(i) ), j = 0; |
|
455
|
|
|
j < arr.length; |
|
456
|
|
|
j++ ) { |
|
457
|
|
|
|
|
458
|
|
|
if ( arr[j] > baseOut - 1 ) { |
|
459
|
|
|
|
|
460
|
|
|
if ( arr[j + 1] == null ) { |
|
461
|
|
|
arr[j + 1] = 0 |
|
462
|
|
|
} |
|
463
|
|
|
arr[j + 1] += arr[j] / baseOut ^ 0; |
|
464
|
|
|
arr[j] %= baseOut |
|
465
|
|
|
} |
|
466
|
|
|
} |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
return arr.reverse() |
|
470
|
|
|
} |
|
471
|
|
|
|
|
472
|
|
|
// Convert array to string. |
|
473
|
|
|
// E.g. arrToStr( [9, 10, 11] ) becomes '9ab' (in bases above 11). |
|
474
|
|
|
function arrToStr( arr ) { |
|
475
|
|
|
var i = 0, |
|
476
|
|
|
arrL = arr.length, |
|
477
|
|
|
str = ''; |
|
478
|
|
|
|
|
479
|
|
|
for ( ; i < arrL; str += DIGITS.charAt( arr[i++] ) ) { |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
return str |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
nStr = nStr.toLowerCase(); |
|
486
|
|
|
|
|
487
|
|
|
/* |
|
488
|
|
|
* If non-integer convert integer part and fraction part separately. |
|
489
|
|
|
* Convert the fraction part as if it is an integer than use division to |
|
490
|
|
|
* reduce it down again to a value less than one. |
|
491
|
|
|
*/ |
|
492
|
|
|
if ( ( e = nStr.indexOf( '.' ) ) > -1 ) { |
|
493
|
|
|
|
|
494
|
|
|
/* |
|
495
|
|
|
* Calculate the power to which to raise the base to get the number |
|
496
|
|
|
* to divide the fraction part by after it has been converted as an |
|
497
|
|
|
* integer to the required base. |
|
498
|
|
|
*/ |
|
499
|
|
|
e = nStr.length - e - 1; |
|
500
|
|
|
|
|
501
|
|
|
// Use toFixed to avoid possible exponential notation. |
|
502
|
|
|
dvs = strToArr( new BigNumber(baseIn)['pow'](e)['toF'](), 10 ); |
|
503
|
|
|
|
|
504
|
|
|
nArr = nStr.split('.'); |
|
505
|
|
|
|
|
506
|
|
|
// Convert the base of the fraction part (as integer). |
|
507
|
|
|
dvd = strToArr( nArr[1] ); |
|
508
|
|
|
|
|
509
|
|
|
// Convert the base of the integer part. |
|
510
|
|
|
nArr = strToArr( nArr[0] ); |
|
511
|
|
|
|
|
512
|
|
|
// Result will be a BigNumber with a value less than 1. |
|
513
|
|
|
fracBN = divide( dvd, dvs, dvd.length - dvs.length, sign, baseOut, |
|
514
|
|
|
|
|
515
|
|
|
// Is least significant digit of integer part an odd number? |
|
516
|
|
|
nArr[nArr.length - 1] & 1 ); |
|
517
|
|
|
|
|
518
|
|
|
fracArr = fracBN['c']; |
|
519
|
|
|
|
|
520
|
|
|
// e can be <= 0 ( if e == 0, fracArr is [0] or [1] ). |
|
521
|
|
|
if ( e = fracBN['e'] ) { |
|
522
|
|
|
|
|
523
|
|
|
// Append zeros according to the exponent of the result. |
|
524
|
|
|
for ( ; ++e; fracArr.unshift(0) ) { |
|
525
|
|
|
} |
|
526
|
|
|
|
|
527
|
|
|
// Append the fraction part to the converted integer part. |
|
528
|
|
|
nStr = arrToStr(nArr) + '.' + arrToStr(fracArr) |
|
529
|
|
|
|
|
530
|
|
|
// fracArr is [1]. |
|
531
|
|
|
// Fraction digits rounded up, so increment last digit of integer part. |
|
532
|
|
|
} else if ( fracArr[0] ) { |
|
533
|
|
|
|
|
534
|
|
|
if ( nArr[ e = nArr.length - 1 ] < baseOut - 1 ) { |
|
535
|
|
|
++nArr[e]; |
|
536
|
|
|
nStr = arrToStr(nArr) |
|
537
|
|
|
} else { |
|
538
|
|
|
nStr = new BigNumber( arrToStr(nArr), |
|
539
|
|
|
baseOut )['plus'](ONE)['toS'](baseOut) |
|
540
|
|
|
} |
|
541
|
|
|
|
|
542
|
|
|
// fracArr is [0]. No fraction digits. |
|
543
|
|
|
} else { |
|
544
|
|
|
nStr = arrToStr(nArr) |
|
545
|
|
|
} |
|
546
|
|
|
} else { |
|
547
|
|
|
|
|
548
|
|
|
// Simple integer. Convert base. |
|
549
|
|
|
nStr = arrToStr( strToArr(nStr) ) |
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
return nStr |
|
553
|
|
|
} |
|
554
|
|
|
|
|
555
|
|
|
|
|
556
|
|
|
// Perform division in the specified base. Called by div and convert. |
|
557
|
|
|
function divide( dvd, dvs, exp, s, base, isOdd ) { |
|
558
|
|
|
var dvsL, dvsT, next, cmp, remI, |
|
559
|
|
|
dvsZ = dvs.slice(), |
|
560
|
|
|
dvdI = dvsL = dvs.length, |
|
561
|
|
|
dvdL = dvd.length, |
|
562
|
|
|
rem = dvd.slice( 0, dvsL ), |
|
563
|
|
|
remL = rem.length, |
|
564
|
|
|
quo = new BigNumber(ONE), |
|
565
|
|
|
qc = quo['c'] = [], |
|
566
|
|
|
qi = 0, |
|
567
|
|
|
dig = DECIMAL_PLACES + ( quo['e'] = exp ) + 1; |
|
568
|
|
|
|
|
569
|
|
|
quo['s'] = s; |
|
570
|
|
|
s = dig < 0 ? 0 : dig; |
|
571
|
|
|
|
|
572
|
|
|
// Add zeros to make remainder as long as divisor. |
|
573
|
|
|
for ( ; remL++ < dvsL; rem.push(0) ) { |
|
574
|
|
|
} |
|
575
|
|
|
|
|
576
|
|
|
// Create version of divisor with leading zero. |
|
577
|
|
|
dvsZ.unshift(0); |
|
578
|
|
|
|
|
579
|
|
|
do { |
|
580
|
|
|
|
|
581
|
|
|
// 'next' is how many times the divisor goes into the current remainder. |
|
582
|
|
|
for ( next = 0; next < base; next++ ) { |
|
583
|
|
|
|
|
584
|
|
|
// Compare divisor and remainder. |
|
585
|
|
|
if ( dvsL != ( remL = rem.length ) ) { |
|
586
|
|
|
cmp = dvsL > remL ? 1 : -1 |
|
587
|
|
|
} else { |
|
588
|
|
|
for ( remI = -1, cmp = 0; ++remI < dvsL; ) { |
|
589
|
|
|
|
|
590
|
|
|
if ( dvs[remI] != rem[remI] ) { |
|
591
|
|
|
cmp = dvs[remI] > rem[remI] ? 1 : -1; |
|
592
|
|
|
break |
|
593
|
|
|
} |
|
594
|
|
|
} |
|
595
|
|
|
} |
|
596
|
|
|
|
|
597
|
|
|
// Subtract divisor from remainder (if divisor < remainder). |
|
598
|
|
|
if ( cmp < 0 ) { |
|
599
|
|
|
|
|
600
|
|
|
// Remainder cannot be more than one digit longer than divisor. |
|
601
|
|
|
// Equalise lengths using divisor with extra leading zero? |
|
602
|
|
|
for ( dvsT = remL == dvsL ? dvs : dvsZ; remL; ) { |
|
603
|
|
|
|
|
604
|
|
|
if ( rem[--remL] < dvsT[remL] ) { |
|
605
|
|
|
|
|
606
|
|
|
for ( remI = remL; |
|
607
|
|
|
remI && !rem[--remI]; |
|
608
|
|
|
rem[remI] = base - 1 ) { |
|
609
|
|
|
} |
|
610
|
|
|
--rem[remI]; |
|
611
|
|
|
rem[remL] += base |
|
612
|
|
|
} |
|
613
|
|
|
rem[remL] -= dvsT[remL] |
|
614
|
|
|
} |
|
615
|
|
|
for ( ; !rem[0]; rem.shift() ) { |
|
616
|
|
|
} |
|
617
|
|
|
} else { |
|
618
|
|
|
break |
|
619
|
|
|
} |
|
620
|
|
|
} |
|
621
|
|
|
|
|
622
|
|
|
// Add the 'next' digit to the result array. |
|
623
|
|
|
qc[qi++] = cmp ? next : ++next; |
|
624
|
|
|
|
|
625
|
|
|
// Update the remainder. |
|
626
|
|
|
rem[0] && cmp |
|
627
|
|
|
? ( rem[remL] = dvd[dvdI] || 0 ) |
|
628
|
|
|
: ( rem = [ dvd[dvdI] ] ) |
|
629
|
|
|
|
|
630
|
|
|
} while ( ( dvdI++ < dvdL || rem[0] != null ) && s-- ); |
|
631
|
|
|
|
|
632
|
|
|
// Leading zero? Do not remove if result is simply zero (qi == 1). |
|
633
|
|
|
if ( !qc[0] && qi != 1) { |
|
634
|
|
|
|
|
635
|
|
|
// There can't be more than one zero. |
|
636
|
|
|
--quo['e']; |
|
637
|
|
|
qc.shift() |
|
638
|
|
|
} |
|
639
|
|
|
|
|
640
|
|
|
// Round? |
|
641
|
|
|
if ( qi > dig ) { |
|
642
|
|
|
rnd( quo, DECIMAL_PLACES, base, isOdd, rem[0] != null ) |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
// Overflow? |
|
646
|
|
|
if ( quo['e'] > MAX_EXP ) { |
|
647
|
|
|
|
|
648
|
|
|
// Infinity. |
|
649
|
|
|
quo['c'] = quo['e'] = null |
|
650
|
|
|
|
|
651
|
|
|
// Underflow? |
|
652
|
|
|
} else if ( quo['e'] < MIN_EXP ) { |
|
653
|
|
|
|
|
654
|
|
|
// Zero. |
|
655
|
|
|
quo['c'] = [quo['e'] = 0] |
|
656
|
|
|
} |
|
657
|
|
|
|
|
658
|
|
|
return quo |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
|
|
662
|
|
|
/* |
|
663
|
|
|
* Return a string representing the value of BigNumber n in normal or |
|
664
|
|
|
* exponential notation rounded to the specified decimal places or |
|
665
|
|
|
* significant digits. |
|
666
|
|
|
* Called by toString, toExponential (exp 1), toFixed, and toPrecision (exp 2). |
|
667
|
|
|
* d is the index (with the value in normal notation) of the digit that may be |
|
668
|
|
|
* rounded up. |
|
669
|
|
|
*/ |
|
670
|
|
|
function format( n, d, exp ) { |
|
671
|
|
|
|
|
672
|
|
|
// Initially, i is the number of decimal places required. |
|
673
|
|
|
var i = d - (n = new BigNumber(n))['e'], |
|
674
|
|
|
c = n['c']; |
|
675
|
|
|
|
|
676
|
|
|
// +-Infinity or NaN? |
|
677
|
|
|
if ( !c ) { |
|
678
|
|
|
return n['toS']() |
|
679
|
|
|
} |
|
680
|
|
|
|
|
681
|
|
|
// Round? |
|
682
|
|
|
if ( c.length > ++d ) { |
|
683
|
|
|
rnd( n, i, 10 ) |
|
684
|
|
|
} |
|
685
|
|
|
|
|
686
|
|
|
// Recalculate d if toFixed as n['e'] may have changed if value rounded up. |
|
687
|
|
|
i = c[0] == 0 ? i + 1 : exp ? d : n['e'] + i + 1; |
|
688
|
|
|
|
|
689
|
|
|
// Append zeros? |
|
690
|
|
|
for ( ; c.length < i; c.push(0) ) { |
|
691
|
|
|
} |
|
692
|
|
|
i = n['e']; |
|
693
|
|
|
|
|
694
|
|
|
/* |
|
695
|
|
|
* toPrecision returns exponential notation if the number of significant |
|
696
|
|
|
* digits specified is less than the number of digits necessary to |
|
697
|
|
|
* represent the integer part of the value in normal notation. |
|
698
|
|
|
*/ |
|
699
|
|
|
return exp == 1 || exp == 2 && ( --d < i || i <= TO_EXP_NEG ) |
|
700
|
|
|
|
|
701
|
|
|
// Exponential notation. |
|
702
|
|
|
? ( n['s'] < 0 && c[0] ? '-' : '' ) + ( c.length > 1 |
|
703
|
|
|
? ( c.splice( 1, 0, '.' ), c.join('') ) |
|
704
|
|
|
: c[0] ) + ( i < 0 ? 'e' : 'e+' ) + i |
|
705
|
|
|
|
|
706
|
|
|
// Normal notation. |
|
707
|
|
|
: n['toS']() |
|
708
|
|
|
} |
|
709
|
|
|
|
|
710
|
|
|
|
|
711
|
|
|
// Round if necessary. |
|
712
|
|
|
// Called by divide, format, setMode and sqrt. |
|
713
|
|
|
function rnd( x, dp, base, isOdd, r) { |
|
714
|
|
|
var xc = x['c'], |
|
715
|
|
|
isNeg = x['s'] < 0, |
|
716
|
|
|
half = base / 2, |
|
717
|
|
|
i = x['e'] + dp + 1, |
|
718
|
|
|
|
|
719
|
|
|
// 'next' is the digit after the digit that may be rounded up. |
|
720
|
|
|
next = xc[i], |
|
721
|
|
|
|
|
722
|
|
|
/* |
|
723
|
|
|
* 'more' is whether there are digits after 'next'. |
|
724
|
|
|
* E.g. |
|
725
|
|
|
* 0.005 (e = -3) to be rounded to 0 decimal places (dp = 0) gives i = -2 |
|
726
|
|
|
* The 'next' digit is zero, and there ARE 'more' digits after it. |
|
727
|
|
|
* 0.5 (e = -1) dp = 0 gives i = 0 |
|
728
|
|
|
* The 'next' digit is 5 and there are no 'more' digits after it. |
|
729
|
|
|
*/ |
|
730
|
|
|
more = r || i < 0 || xc[i + 1] != null; |
|
731
|
|
|
|
|
732
|
|
|
r = ROUNDING_MODE < 4 |
|
733
|
|
|
? ( next != null || more ) && |
|
734
|
|
|
( ROUNDING_MODE == 0 || |
|
735
|
|
|
ROUNDING_MODE == 2 && !isNeg || |
|
736
|
|
|
ROUNDING_MODE == 3 && isNeg ) |
|
737
|
|
|
: next > half || next == half && |
|
738
|
|
|
( ROUNDING_MODE == 4 || more || |
|
739
|
|
|
|
|
740
|
|
|
/* |
|
741
|
|
|
* isOdd is used in base conversion and refers to the least significant |
|
742
|
|
|
* digit of the integer part of the value to be converted. The fraction |
|
743
|
|
|
* part is rounded by this method separately from the integer part. |
|
744
|
|
|
*/ |
|
745
|
|
|
ROUNDING_MODE == 6 && ( xc[i - 1] & 1 || !dp && isOdd ) || |
|
746
|
|
|
ROUNDING_MODE == 7 && !isNeg || |
|
747
|
|
|
ROUNDING_MODE == 8 && isNeg ); |
|
748
|
|
|
|
|
749
|
|
|
if ( i < 1 || !xc[0] ) { |
|
750
|
|
|
xc.length = 0; |
|
751
|
|
|
xc.push(0); |
|
752
|
|
|
|
|
753
|
|
|
if ( r ) { |
|
754
|
|
|
|
|
755
|
|
|
// 1, 0.1, 0.01, 0.001, 0.0001 etc. |
|
756
|
|
|
xc[0] = 1; |
|
757
|
|
|
x['e'] = -dp |
|
758
|
|
|
} else { |
|
759
|
|
|
|
|
760
|
|
|
// Zero. |
|
761
|
|
|
x['e'] = 0 |
|
762
|
|
|
} |
|
763
|
|
|
|
|
764
|
|
|
return x |
|
765
|
|
|
} |
|
766
|
|
|
|
|
767
|
|
|
// Remove any digits after the required decimal places. |
|
768
|
|
|
xc.length = i--; |
|
769
|
|
|
|
|
770
|
|
|
// Round up? |
|
771
|
|
|
if ( r ) { |
|
772
|
|
|
|
|
773
|
|
|
// Rounding up may mean the previous digit has to be rounded up and so on. |
|
774
|
|
|
for ( --base; ++xc[i] > base; ) { |
|
775
|
|
|
xc[i] = 0; |
|
776
|
|
|
|
|
777
|
|
|
if ( !i-- ) { |
|
778
|
|
|
++x['e']; |
|
779
|
|
|
xc.unshift(1) |
|
780
|
|
|
} |
|
781
|
|
|
} |
|
782
|
|
|
} |
|
783
|
|
|
|
|
784
|
|
|
// Remove trailing zeros. |
|
785
|
|
|
for ( i = xc.length; !xc[--i]; xc.pop() ) { |
|
786
|
|
|
} |
|
787
|
|
|
|
|
788
|
|
|
return x |
|
789
|
|
|
} |
|
790
|
|
|
|
|
791
|
|
|
|
|
792
|
|
|
// Round after setting the appropriate rounding mode. |
|
793
|
|
|
// Handles ceil, floor and round. |
|
794
|
|
|
function setMode( x, dp, rm ) { |
|
795
|
|
|
var r = ROUNDING_MODE; |
|
796
|
|
|
|
|
797
|
|
|
ROUNDING_MODE = rm; |
|
798
|
|
|
x = new BigNumber(x); |
|
799
|
|
|
x['c'] && rnd( x, dp, 10 ); |
|
800
|
|
|
ROUNDING_MODE = r; |
|
801
|
|
|
|
|
802
|
|
|
return x |
|
803
|
|
|
} |
|
804
|
|
|
|
|
805
|
|
|
|
|
806
|
|
|
// PROTOTYPE/INSTANCE METHODS |
|
807
|
|
|
|
|
808
|
|
|
|
|
809
|
|
|
/* |
|
810
|
|
|
* Return a new BigNumber whose value is the absolute value of this BigNumber. |
|
811
|
|
|
*/ |
|
812
|
|
|
P['abs'] = P['absoluteValue'] = function () { |
|
813
|
|
|
var x = new BigNumber(this); |
|
814
|
|
|
|
|
815
|
|
|
if ( x['s'] < 0 ) { |
|
816
|
|
|
x['s'] = 1 |
|
817
|
|
|
} |
|
818
|
|
|
|
|
819
|
|
|
return x |
|
820
|
|
|
}; |
|
821
|
|
|
|
|
822
|
|
|
|
|
823
|
|
|
/* |
|
824
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber |
|
825
|
|
|
* rounded to a whole number in the direction of Infinity. |
|
826
|
|
|
*/ |
|
827
|
|
|
P['ceil'] = function () { |
|
828
|
|
|
return setMode( this, 0, 2 ) |
|
829
|
|
|
}; |
|
830
|
|
|
|
|
831
|
|
|
|
|
832
|
|
|
/* |
|
833
|
|
|
* Return |
|
834
|
|
|
* 1 if the value of this BigNumber is greater than the value of BigNumber(y, b), |
|
835
|
|
|
* -1 if the value of this BigNumber is less than the value of BigNumber(y, b), |
|
836
|
|
|
* 0 if they have the same value, |
|
837
|
|
|
* or null if the value of either is NaN. |
|
838
|
|
|
*/ |
|
839
|
|
|
P['comparedTo'] = P['cmp'] = function ( y, b ) { |
|
840
|
|
|
var a, |
|
841
|
|
|
x = this, |
|
842
|
|
|
xc = x['c'], |
|
843
|
|
|
yc = ( id = -id, y = new BigNumber( y, b ) )['c'], |
|
844
|
|
|
i = x['s'], |
|
845
|
|
|
j = y['s'], |
|
846
|
|
|
k = x['e'], |
|
847
|
|
|
l = y['e']; |
|
848
|
|
|
|
|
849
|
|
|
// Either NaN? |
|
850
|
|
|
if ( !i || !j ) { |
|
851
|
|
|
return null |
|
852
|
|
|
} |
|
853
|
|
|
|
|
854
|
|
|
a = xc && !xc[0], b = yc && !yc[0]; |
|
855
|
|
|
|
|
856
|
|
|
// Either zero? |
|
857
|
|
|
if ( a || b ) { |
|
858
|
|
|
return a ? b ? 0 : -j : i |
|
859
|
|
|
} |
|
860
|
|
|
|
|
861
|
|
|
// Signs differ? |
|
862
|
|
|
if ( i != j ) { |
|
863
|
|
|
return i |
|
864
|
|
|
} |
|
865
|
|
|
|
|
866
|
|
|
// Either Infinity? |
|
867
|
|
|
if ( a = i < 0, b = k == l, !xc || !yc ) { |
|
868
|
|
|
return b ? 0 : !xc ^ a ? 1 : -1 |
|
869
|
|
|
} |
|
870
|
|
|
|
|
871
|
|
|
// Compare exponents. |
|
872
|
|
|
if ( !b ) { |
|
873
|
|
|
return k > l ^ a ? 1 : -1 |
|
874
|
|
|
} |
|
875
|
|
|
|
|
876
|
|
|
// Compare digit by digit. |
|
877
|
|
|
for ( i = -1, |
|
878
|
|
|
j = ( k = xc.length ) < ( l = yc.length ) ? k : l; |
|
879
|
|
|
++i < j; ) { |
|
880
|
|
|
|
|
881
|
|
|
if ( xc[i] != yc[i] ) { |
|
882
|
|
|
return xc[i] > yc[i] ^ a ? 1 : -1 |
|
883
|
|
|
} |
|
884
|
|
|
} |
|
885
|
|
|
// Compare lengths. |
|
886
|
|
|
return k == l ? 0 : k > l ^ a ? 1 : -1 |
|
887
|
|
|
}; |
|
888
|
|
|
|
|
889
|
|
|
|
|
890
|
|
|
/* |
|
891
|
|
|
* n / 0 = I |
|
892
|
|
|
* n / N = N |
|
893
|
|
|
* n / I = 0 |
|
894
|
|
|
* 0 / n = 0 |
|
895
|
|
|
* 0 / 0 = N |
|
896
|
|
|
* 0 / N = N |
|
897
|
|
|
* 0 / I = 0 |
|
898
|
|
|
* N / n = N |
|
899
|
|
|
* N / 0 = N |
|
900
|
|
|
* N / N = N |
|
901
|
|
|
* N / I = N |
|
902
|
|
|
* I / n = I |
|
903
|
|
|
* I / 0 = I |
|
904
|
|
|
* I / N = N |
|
905
|
|
|
* I / I = N |
|
906
|
|
|
* |
|
907
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber |
|
908
|
|
|
* divided by the value of BigNumber(y, b), rounded according to |
|
909
|
|
|
* DECIMAL_PLACES and ROUNDING_MODE. |
|
910
|
|
|
*/ |
|
911
|
|
|
P['dividedBy'] = P['div'] = function ( y, b ) { |
|
912
|
|
|
var xc = this['c'], |
|
913
|
|
|
xe = this['e'], |
|
914
|
|
|
xs = this['s'], |
|
915
|
|
|
yc = ( id = 2, y = new BigNumber( y, b ) )['c'], |
|
916
|
|
|
ye = y['e'], |
|
917
|
|
|
ys = y['s'], |
|
918
|
|
|
s = xs == ys ? 1 : -1; |
|
919
|
|
|
|
|
920
|
|
|
// Either NaN/Infinity/0? |
|
921
|
|
|
return !xe && ( !xc || !xc[0] ) || !ye && ( !yc || !yc[0] ) |
|
922
|
|
|
|
|
923
|
|
|
// Either NaN? |
|
924
|
|
|
? new BigNumber( !xs || !ys || |
|
925
|
|
|
|
|
926
|
|
|
// Both 0 or both Infinity? |
|
927
|
|
|
( xc ? yc && xc[0] == yc[0] : !yc ) |
|
928
|
|
|
|
|
929
|
|
|
// Return NaN. |
|
930
|
|
|
? NaN |
|
931
|
|
|
|
|
932
|
|
|
// x is 0 or y is Infinity? |
|
933
|
|
|
: xc && xc[0] == 0 || !yc |
|
934
|
|
|
|
|
935
|
|
|
// Return +-0. |
|
936
|
|
|
? s * 0 |
|
937
|
|
|
|
|
938
|
|
|
// y is 0. Return +-Infinity. |
|
939
|
|
|
: s / 0 ) |
|
940
|
|
|
|
|
941
|
|
|
: divide( xc, yc, xe - ye, s, 10 ) |
|
942
|
|
|
}; |
|
943
|
|
|
|
|
944
|
|
|
|
|
945
|
|
|
/* |
|
946
|
|
|
* Return true if the value of this BigNumber is equal to the value of |
|
947
|
|
|
* BigNumber(n, b), otherwise returns false. |
|
948
|
|
|
*/ |
|
949
|
|
|
P['equals'] = P['eq'] = function ( n, b ) { |
|
950
|
|
|
id = 3; |
|
951
|
|
|
return this['cmp']( n, b ) === 0 |
|
952
|
|
|
}; |
|
953
|
|
|
|
|
954
|
|
|
|
|
955
|
|
|
/* |
|
956
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber |
|
957
|
|
|
* rounded to a whole number in the direction of -Infinity. |
|
958
|
|
|
*/ |
|
959
|
|
|
P['floor'] = function () { |
|
960
|
|
|
return setMode( this, 0, 3 ) |
|
961
|
|
|
}; |
|
962
|
|
|
|
|
963
|
|
|
|
|
964
|
|
|
/* |
|
965
|
|
|
* Return true if the value of this BigNumber is greater than the value of |
|
966
|
|
|
* BigNumber(n, b), otherwise returns false. |
|
967
|
|
|
*/ |
|
968
|
|
|
P['greaterThan'] = P['gt'] = function ( n, b ) { |
|
969
|
|
|
id = 4; |
|
970
|
|
|
return this['cmp']( n, b ) > 0 |
|
971
|
|
|
}; |
|
972
|
|
|
|
|
973
|
|
|
|
|
974
|
|
|
/* |
|
975
|
|
|
* Return true if the value of this BigNumber is greater than or equal to |
|
976
|
|
|
* the value of BigNumber(n, b), otherwise returns false. |
|
977
|
|
|
*/ |
|
978
|
|
|
P['greaterThanOrEqualTo'] = P['gte'] = function ( n, b ) { |
|
979
|
|
|
id = 5; |
|
980
|
|
|
return ( b = this['cmp']( n, b ) ) == 1 || b === 0 |
|
981
|
|
|
}; |
|
982
|
|
|
|
|
983
|
|
|
|
|
984
|
|
|
/* |
|
985
|
|
|
* Return true if the value of this BigNumber is a finite number, otherwise |
|
986
|
|
|
* returns false. |
|
987
|
|
|
*/ |
|
988
|
|
|
P['isFinite'] = P['isF'] = function () { |
|
989
|
|
|
return !!this['c'] |
|
990
|
|
|
}; |
|
991
|
|
|
|
|
992
|
|
|
|
|
993
|
|
|
/* |
|
994
|
|
|
* Return true if the value of this BigNumber is NaN, otherwise returns |
|
995
|
|
|
* false. |
|
996
|
|
|
*/ |
|
997
|
|
|
P['isNaN'] = function () { |
|
998
|
|
|
return !this['s'] |
|
999
|
|
|
}; |
|
1000
|
|
|
|
|
1001
|
|
|
|
|
1002
|
|
|
/* |
|
1003
|
|
|
* Return true if the value of this BigNumber is negative, otherwise |
|
1004
|
|
|
* returns false. |
|
1005
|
|
|
*/ |
|
1006
|
|
|
P['isNegative'] = P['isNeg'] = function () { |
|
1007
|
|
|
return this['s'] < 0 |
|
1008
|
|
|
}; |
|
1009
|
|
|
|
|
1010
|
|
|
|
|
1011
|
|
|
/* |
|
1012
|
|
|
* Return true if the value of this BigNumber is 0 or -0, otherwise returns |
|
1013
|
|
|
* false. |
|
1014
|
|
|
*/ |
|
1015
|
|
|
P['isZero'] = P['isZ'] = function () { |
|
1016
|
|
|
return !!this['c'] && this['c'][0] == 0 |
|
1017
|
|
|
}; |
|
1018
|
|
|
|
|
1019
|
|
|
|
|
1020
|
|
|
/* |
|
1021
|
|
|
* Return true if the value of this BigNumber is less than the value of |
|
1022
|
|
|
* BigNumber(n, b), otherwise returns false. |
|
1023
|
|
|
*/ |
|
1024
|
|
|
P['lessThan'] = P['lt'] = function ( n, b ) { |
|
1025
|
|
|
id = 6; |
|
1026
|
|
|
return this['cmp']( n, b ) < 0 |
|
1027
|
|
|
}; |
|
1028
|
|
|
|
|
1029
|
|
|
|
|
1030
|
|
|
/* |
|
1031
|
|
|
* Return true if the value of this BigNumber is less than or equal to the |
|
1032
|
|
|
* value of BigNumber(n, b), otherwise returns false. |
|
1033
|
|
|
*/ |
|
1034
|
|
|
P['lessThanOrEqualTo'] = P['lte'] = function ( n, b ) { |
|
1035
|
|
|
id = 7; |
|
1036
|
|
|
return ( b = this['cmp']( n, b ) ) == -1 || b === 0 |
|
1037
|
|
|
}; |
|
1038
|
|
|
|
|
1039
|
|
|
|
|
1040
|
|
|
/* |
|
1041
|
|
|
* n - 0 = n |
|
1042
|
|
|
* n - N = N |
|
1043
|
|
|
* n - I = -I |
|
1044
|
|
|
* 0 - n = -n |
|
1045
|
|
|
* 0 - 0 = 0 |
|
1046
|
|
|
* 0 - N = N |
|
1047
|
|
|
* 0 - I = -I |
|
1048
|
|
|
* N - n = N |
|
1049
|
|
|
* N - 0 = N |
|
1050
|
|
|
* N - N = N |
|
1051
|
|
|
* N - I = N |
|
1052
|
|
|
* I - n = I |
|
1053
|
|
|
* I - 0 = I |
|
1054
|
|
|
* I - N = N |
|
1055
|
|
|
* I - I = N |
|
1056
|
|
|
* |
|
1057
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber minus |
|
1058
|
|
|
* the value of BigNumber(y, b). |
|
1059
|
|
|
*/ |
|
1060
|
|
|
P['minus'] = function ( y, b ) { |
|
1061
|
|
|
var d, i, j, xLTy, |
|
1062
|
|
|
x = this, |
|
1063
|
|
|
a = x['s']; |
|
1064
|
|
|
|
|
1065
|
|
|
b = ( id = 8, y = new BigNumber( y, b ) )['s']; |
|
1066
|
|
|
|
|
1067
|
|
|
// Either NaN? |
|
1068
|
|
|
if ( !a || !b ) { |
|
1069
|
|
|
return new BigNumber(NaN) |
|
1070
|
|
|
} |
|
1071
|
|
|
|
|
1072
|
|
|
// Signs differ? |
|
1073
|
|
|
if ( a != b ) { |
|
1074
|
|
|
return y['s'] = -b, x['plus'](y) |
|
1075
|
|
|
} |
|
1076
|
|
|
|
|
1077
|
|
|
var xc = x['c'], |
|
1078
|
|
|
xe = x['e'], |
|
1079
|
|
|
yc = y['c'], |
|
1080
|
|
|
ye = y['e']; |
|
1081
|
|
|
|
|
1082
|
|
|
if ( !xe || !ye ) { |
|
1083
|
|
|
|
|
1084
|
|
|
// Either Infinity? |
|
1085
|
|
|
if ( !xc || !yc ) { |
|
1086
|
|
|
return xc ? ( y['s'] = -b, y ) : new BigNumber( yc ? x : NaN ) |
|
1087
|
|
|
} |
|
1088
|
|
|
|
|
1089
|
|
|
// Either zero? |
|
1090
|
|
|
if ( !xc[0] || !yc[0] ) { |
|
1091
|
|
|
|
|
1092
|
|
|
// y is non-zero? |
|
1093
|
|
|
return yc[0] |
|
1094
|
|
|
? ( y['s'] = -b, y ) |
|
1095
|
|
|
|
|
1096
|
|
|
// x is non-zero? |
|
1097
|
|
|
: new BigNumber( xc[0] |
|
1098
|
|
|
? x |
|
1099
|
|
|
|
|
1100
|
|
|
// Both are zero. |
|
1101
|
|
|
: 0 ) |
|
1102
|
|
|
} |
|
1103
|
|
|
} |
|
1104
|
|
|
|
|
1105
|
|
|
// Determine which is the bigger number. |
|
1106
|
|
|
// Prepend zeros to equalise exponents. |
|
1107
|
|
|
if ( xc = xc.slice(), a = xe - ye ) { |
|
1108
|
|
|
d = ( xLTy = a < 0 ) ? ( a = -a, xc ) : ( ye = xe, yc ); |
|
1109
|
|
|
|
|
1110
|
|
|
for ( d.reverse(), b = a; b--; d.push(0) ) { |
|
1111
|
|
|
} |
|
1112
|
|
|
d.reverse() |
|
1113
|
|
|
} else { |
|
1114
|
|
|
|
|
1115
|
|
|
// Exponents equal. Check digit by digit. |
|
1116
|
|
|
j = ( ( xLTy = xc.length < yc.length ) ? xc : yc ).length; |
|
1117
|
|
|
|
|
1118
|
|
|
for ( a = b = 0; b < j; b++ ) { |
|
1119
|
|
|
|
|
1120
|
|
|
if ( xc[b] != yc[b] ) { |
|
1121
|
|
|
xLTy = xc[b] < yc[b]; |
|
1122
|
|
|
break |
|
1123
|
|
|
} |
|
1124
|
|
|
} |
|
1125
|
|
|
} |
|
1126
|
|
|
|
|
1127
|
|
|
// x < y? Point xc to the array of the bigger number. |
|
1128
|
|
|
if ( xLTy ) { |
|
1129
|
|
|
d = xc, xc = yc, yc = d; |
|
1130
|
|
|
y['s'] = -y['s'] |
|
1131
|
|
|
} |
|
1132
|
|
|
|
|
1133
|
|
|
/* |
|
1134
|
|
|
* Append zeros to xc if shorter. No need to add zeros to yc if shorter |
|
1135
|
|
|
* as subtraction only needs to start at yc.length. |
|
1136
|
|
|
*/ |
|
1137
|
|
|
if ( ( b = -( ( j = xc.length ) - yc.length ) ) > 0 ) { |
|
1138
|
|
|
|
|
1139
|
|
|
for ( ; b--; xc[j++] = 0 ) { |
|
1140
|
|
|
} |
|
1141
|
|
|
} |
|
1142
|
|
|
|
|
1143
|
|
|
// Subtract yc from xc. |
|
1144
|
|
|
for ( b = yc.length; b > a; ){ |
|
1145
|
|
|
|
|
1146
|
|
|
if ( xc[--b] < yc[b] ) { |
|
1147
|
|
|
|
|
1148
|
|
|
for ( i = b; i && !xc[--i]; xc[i] = 9 ) { |
|
1149
|
|
|
} |
|
1150
|
|
|
--xc[i]; |
|
1151
|
|
|
xc[b] += 10 |
|
1152
|
|
|
} |
|
1153
|
|
|
xc[b] -= yc[b] |
|
1154
|
|
|
} |
|
1155
|
|
|
|
|
1156
|
|
|
// Remove trailing zeros. |
|
1157
|
|
|
for ( ; xc[--j] == 0; xc.pop() ) { |
|
1158
|
|
|
} |
|
1159
|
|
|
|
|
1160
|
|
|
// Remove leading zeros and adjust exponent accordingly. |
|
1161
|
|
|
for ( ; xc[0] == 0; xc.shift(), --ye ) { |
|
1162
|
|
|
} |
|
1163
|
|
|
|
|
1164
|
|
|
/* |
|
1165
|
|
|
* No need to check for Infinity as +x - +y != Infinity && -x - -y != Infinity |
|
1166
|
|
|
* when neither x or y are Infinity. |
|
1167
|
|
|
*/ |
|
1168
|
|
|
|
|
1169
|
|
|
// Underflow? |
|
1170
|
|
|
if ( ye < MIN_EXP || !xc[0] ) { |
|
1171
|
|
|
|
|
1172
|
|
|
// Result must be zero. |
|
1173
|
|
|
xc = [ye = 0] |
|
1174
|
|
|
} |
|
1175
|
|
|
|
|
1176
|
|
|
return y['c'] = xc, y['e'] = ye, y |
|
1177
|
|
|
}; |
|
1178
|
|
|
|
|
1179
|
|
|
|
|
1180
|
|
|
/* |
|
1181
|
|
|
* n % 0 = N |
|
1182
|
|
|
* n % N = N |
|
1183
|
|
|
* 0 % n = 0 |
|
1184
|
|
|
* -0 % n = -0 |
|
1185
|
|
|
* 0 % 0 = N |
|
1186
|
|
|
* 0 % N = N |
|
1187
|
|
|
* N % n = N |
|
1188
|
|
|
* N % 0 = N |
|
1189
|
|
|
* N % N = N |
|
1190
|
|
|
* |
|
1191
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber modulo |
|
1192
|
|
|
* the value of BigNumber(y, b). |
|
1193
|
|
|
*/ |
|
1194
|
|
|
P['modulo'] = P['mod'] = function ( y, b ) { |
|
1195
|
|
|
var x = this, |
|
1196
|
|
|
xc = x['c'], |
|
1197
|
|
|
yc = ( id = 9, y = new BigNumber( y, b ) )['c'], |
|
1198
|
|
|
i = x['s'], |
|
1199
|
|
|
j = y['s']; |
|
1200
|
|
|
|
|
1201
|
|
|
// Is x or y NaN, or y zero? |
|
1202
|
|
|
b = !i || !j || yc && !yc[0]; |
|
1203
|
|
|
|
|
1204
|
|
|
if ( b || xc && !xc[0] ) { |
|
1205
|
|
|
return new BigNumber( b ? NaN : x ) |
|
1206
|
|
|
} |
|
1207
|
|
|
|
|
1208
|
|
|
x['s'] = y['s'] = 1; |
|
1209
|
|
|
b = y['cmp'](x) == 1; |
|
1210
|
|
|
x['s'] = i, y['s'] = j; |
|
1211
|
|
|
|
|
1212
|
|
|
return b |
|
1213
|
|
|
? new BigNumber(x) |
|
1214
|
|
|
: ( i = DECIMAL_PLACES, j = ROUNDING_MODE, |
|
1215
|
|
|
DECIMAL_PLACES = 0, ROUNDING_MODE = 1, |
|
1216
|
|
|
x = x['div'](y), |
|
1217
|
|
|
DECIMAL_PLACES = i, ROUNDING_MODE = j, |
|
1218
|
|
|
this['minus']( x['times'](y) ) ) |
|
1219
|
|
|
}; |
|
1220
|
|
|
|
|
1221
|
|
|
|
|
1222
|
|
|
/* |
|
1223
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber |
|
1224
|
|
|
* negated, i.e. multiplied by -1. |
|
1225
|
|
|
*/ |
|
1226
|
|
|
P['negated'] = P['neg'] = function () { |
|
1227
|
|
|
var x = new BigNumber(this); |
|
1228
|
|
|
|
|
1229
|
|
|
return x['s'] = -x['s'] || null, x |
|
1230
|
|
|
}; |
|
1231
|
|
|
|
|
1232
|
|
|
|
|
1233
|
|
|
/* |
|
1234
|
|
|
* n + 0 = n |
|
1235
|
|
|
* n + N = N |
|
1236
|
|
|
* n + I = I |
|
1237
|
|
|
* 0 + n = n |
|
1238
|
|
|
* 0 + 0 = 0 |
|
1239
|
|
|
* 0 + N = N |
|
1240
|
|
|
* 0 + I = I |
|
1241
|
|
|
* N + n = N |
|
1242
|
|
|
* N + 0 = N |
|
1243
|
|
|
* N + N = N |
|
1244
|
|
|
* N + I = N |
|
1245
|
|
|
* I + n = I |
|
1246
|
|
|
* I + 0 = I |
|
1247
|
|
|
* I + N = N |
|
1248
|
|
|
* I + I = I |
|
1249
|
|
|
* |
|
1250
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber plus |
|
1251
|
|
|
* the value of BigNumber(y, b). |
|
1252
|
|
|
*/ |
|
1253
|
|
|
P['plus'] = function ( y, b ) { |
|
1254
|
|
|
var d, |
|
1255
|
|
|
x = this, |
|
1256
|
|
|
a = x['s']; |
|
1257
|
|
|
|
|
1258
|
|
|
b = ( id = 10, y = new BigNumber( y, b ) )['s']; |
|
1259
|
|
|
|
|
1260
|
|
|
// Either NaN? |
|
1261
|
|
|
if ( !a || !b ) { |
|
1262
|
|
|
return new BigNumber(NaN) |
|
1263
|
|
|
} |
|
1264
|
|
|
|
|
1265
|
|
|
// Signs differ? |
|
1266
|
|
|
if ( a != b ) { |
|
1267
|
|
|
return y['s'] = -b, x['minus'](y) |
|
1268
|
|
|
} |
|
1269
|
|
|
|
|
1270
|
|
|
var xe = x['e'], |
|
1271
|
|
|
xc = x['c'], |
|
1272
|
|
|
ye = y['e'], |
|
1273
|
|
|
yc = y['c']; |
|
1274
|
|
|
|
|
1275
|
|
|
if ( !xe || !ye ) { |
|
1276
|
|
|
|
|
1277
|
|
|
// Either Infinity? |
|
1278
|
|
|
if ( !xc || !yc ) { |
|
1279
|
|
|
|
|
1280
|
|
|
// Return +-Infinity. |
|
1281
|
|
|
return new BigNumber( a / 0 ) |
|
1282
|
|
|
} |
|
1283
|
|
|
|
|
1284
|
|
|
// Either zero? |
|
1285
|
|
|
if ( !xc[0] || !yc[0] ) { |
|
1286
|
|
|
|
|
1287
|
|
|
// y is non-zero? |
|
1288
|
|
|
return yc[0] |
|
1289
|
|
|
? y |
|
1290
|
|
|
: new BigNumber( xc[0] |
|
1291
|
|
|
|
|
1292
|
|
|
// x is non-zero? |
|
1293
|
|
|
? x |
|
1294
|
|
|
|
|
1295
|
|
|
// Both are zero. Return zero. |
|
1296
|
|
|
: a * 0 ) |
|
1297
|
|
|
} |
|
1298
|
|
|
} |
|
1299
|
|
|
|
|
1300
|
|
|
// Prepend zeros to equalise exponents. |
|
1301
|
|
|
// Note: Faster to use reverse then do unshifts. |
|
1302
|
|
|
if ( xc = xc.slice(), a = xe - ye ) { |
|
1303
|
|
|
d = a > 0 ? ( ye = xe, yc ) : ( a = -a, xc ); |
|
1304
|
|
|
|
|
1305
|
|
|
for ( d.reverse(); a--; d.push(0) ) { |
|
1306
|
|
|
} |
|
1307
|
|
|
d.reverse() |
|
1308
|
|
|
} |
|
1309
|
|
|
|
|
1310
|
|
|
// Point xc to the longer array. |
|
1311
|
|
|
if ( xc.length - yc.length < 0 ) { |
|
1312
|
|
|
d = yc, yc = xc, xc = d |
|
1313
|
|
|
} |
|
1314
|
|
|
|
|
1315
|
|
|
/* |
|
1316
|
|
|
* Only start adding at yc.length - 1 as the |
|
1317
|
|
|
* further digits of xc can be left as they are. |
|
1318
|
|
|
*/ |
|
1319
|
|
|
for ( a = yc.length, b = 0; a; |
|
1320
|
|
|
b = ( xc[--a] = xc[a] + yc[a] + b ) / 10 ^ 0, xc[a] %= 10 ) { |
|
1321
|
|
|
} |
|
1322
|
|
|
|
|
1323
|
|
|
// No need to check for zero, as +x + +y != 0 && -x + -y != 0 |
|
1324
|
|
|
|
|
1325
|
|
|
if ( b ) { |
|
1326
|
|
|
xc.unshift(b); |
|
1327
|
|
|
|
|
1328
|
|
|
// Overflow? (MAX_EXP + 1 possible) |
|
1329
|
|
|
if ( ++ye > MAX_EXP ) { |
|
1330
|
|
|
|
|
1331
|
|
|
// Infinity. |
|
1332
|
|
|
xc = ye = null |
|
1333
|
|
|
} |
|
1334
|
|
|
} |
|
1335
|
|
|
|
|
1336
|
|
|
// Remove trailing zeros. |
|
1337
|
|
|
for ( a = xc.length; xc[--a] == 0; xc.pop() ) { |
|
1338
|
|
|
} |
|
1339
|
|
|
|
|
1340
|
|
|
return y['c'] = xc, y['e'] = ye, y |
|
1341
|
|
|
}; |
|
1342
|
|
|
|
|
1343
|
|
|
|
|
1344
|
|
|
/* |
|
1345
|
|
|
* Return a BigNumber whose value is the value of this BigNumber raised to |
|
1346
|
|
|
* the power e. If e is negative round according to DECIMAL_PLACES and |
|
1347
|
|
|
* ROUNDING_MODE. |
|
1348
|
|
|
* |
|
1349
|
|
|
* e {number} Integer, -MAX_POWER to MAX_POWER inclusive. |
|
1350
|
|
|
*/ |
|
1351
|
|
|
P['toPower'] = P['pow'] = function ( e ) { |
|
1352
|
|
|
|
|
1353
|
|
|
// e to integer, avoiding NaN or Infinity becoming 0. |
|
1354
|
|
|
var i = e * 0 == 0 ? e | 0 : e, |
|
1355
|
|
|
x = new BigNumber(this), |
|
1356
|
|
|
y = new BigNumber(ONE); |
|
1357
|
|
|
|
|
1358
|
|
|
// Use Math.pow? |
|
1359
|
|
|
// Pass +-Infinity for out of range exponents. |
|
1360
|
|
|
if ( ( ( ( outOfRange = e < -MAX_POWER || e > MAX_POWER ) && |
|
1361
|
|
|
(i = e * 1 / 0) ) || |
|
1362
|
|
|
|
|
1363
|
|
|
/* |
|
1364
|
|
|
* Any exponent that fails the parse becomes NaN. |
|
1365
|
|
|
* |
|
1366
|
|
|
* Include 'e !== 0' because on Opera -0 == parseFloat(-0) is false, |
|
1367
|
|
|
* despite -0 === parseFloat(-0) && -0 == parseFloat('-0') is true. |
|
1368
|
|
|
*/ |
|
1369
|
|
|
parse(e) != e && e !== 0 && !(i = NaN) ) && |
|
1370
|
|
|
|
|
1371
|
|
|
// 'pow() exponent not an integer: {e}' |
|
1372
|
|
|
// 'pow() exponent out of range: {e}' |
|
1373
|
|
|
!ifExceptionsThrow( e, 'exponent', 'pow' ) || |
|
1374
|
|
|
|
|
1375
|
|
|
// Pass zero to Math.pow, as any value to the power zero is 1. |
|
1376
|
|
|
!i ) { |
|
1377
|
|
|
|
|
1378
|
|
|
// i is +-Infinity, NaN or 0. |
|
1379
|
|
|
return new BigNumber( Math.pow( x['toS'](), i ) ) |
|
1380
|
|
|
} |
|
1381
|
|
|
|
|
1382
|
|
|
for ( i = i < 0 ? -i : i; ; ) { |
|
1383
|
|
|
|
|
1384
|
|
|
if ( i & 1 ) { |
|
1385
|
|
|
y = y['times'](x) |
|
1386
|
|
|
} |
|
1387
|
|
|
i >>= 1; |
|
1388
|
|
|
|
|
1389
|
|
|
if ( !i ) { |
|
1390
|
|
|
break |
|
1391
|
|
|
} |
|
1392
|
|
|
x = x['times'](x) |
|
1393
|
|
|
} |
|
1394
|
|
|
|
|
1395
|
|
|
return e < 0 ? ONE['div'](y) : y |
|
1396
|
|
|
}; |
|
1397
|
|
|
|
|
1398
|
|
|
|
|
1399
|
|
|
/* |
|
1400
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber |
|
1401
|
|
|
* rounded to a maximum of dp decimal places using rounding mode rm, or to |
|
1402
|
|
|
* DECIMAL_PLACES and ROUNDING_MODE respectively if omitted. |
|
1403
|
|
|
* |
|
1404
|
|
|
* [dp] {number} Integer, 0 to MAX inclusive. |
|
1405
|
|
|
* [rm] {number} Integer, 0 to 8 inclusive. |
|
1406
|
|
|
*/ |
|
1407
|
|
|
P['round'] = function ( dp, rm ) { |
|
1408
|
|
|
|
|
1409
|
|
|
dp = dp == null || ( ( ( outOfRange = dp < 0 || dp > MAX ) || |
|
1410
|
|
|
parse(dp) != dp ) && |
|
1411
|
|
|
|
|
1412
|
|
|
// 'round() decimal places out of range: {dp}' |
|
1413
|
|
|
// 'round() decimal places not an integer: {dp}' |
|
1414
|
|
|
!ifExceptionsThrow( dp, 'decimal places', 'round' ) ) |
|
1415
|
|
|
? 0 |
|
1416
|
|
|
: dp | 0; |
|
1417
|
|
|
|
|
1418
|
|
|
rm = rm == null || ( ( ( outOfRange = rm < 0 || rm > 8 ) || |
|
1419
|
|
|
|
|
1420
|
|
|
// Include '&& rm !== 0' because with Opera -0 == parseFloat(-0) is false. |
|
1421
|
|
|
parse(rm) != rm && rm !== 0 ) && |
|
1422
|
|
|
|
|
1423
|
|
|
// 'round() mode not an integer: {rm}' |
|
1424
|
|
|
// 'round() mode out of range: {rm}' |
|
1425
|
|
|
!ifExceptionsThrow( rm, 'mode', 'round' ) ) |
|
1426
|
|
|
? ROUNDING_MODE |
|
1427
|
|
|
: rm | 0; |
|
1428
|
|
|
|
|
1429
|
|
|
return setMode( this, dp, rm ) |
|
1430
|
|
|
}; |
|
1431
|
|
|
|
|
1432
|
|
|
|
|
1433
|
|
|
/* |
|
1434
|
|
|
* sqrt(-n) = N |
|
1435
|
|
|
* sqrt( N) = N |
|
1436
|
|
|
* sqrt(-I) = N |
|
1437
|
|
|
* sqrt( I) = I |
|
1438
|
|
|
* sqrt( 0) = 0 |
|
1439
|
|
|
* sqrt(-0) = -0 |
|
1440
|
|
|
* |
|
1441
|
|
|
* Return a new BigNumber whose value is the square root of the value of |
|
1442
|
|
|
* this BigNumber, rounded according to DECIMAL_PLACES and ROUNDING_MODE. |
|
1443
|
|
|
*/ |
|
1444
|
|
|
P['squareRoot'] = P['sqrt'] = function () { |
|
1445
|
|
|
var estimate, r, approx, |
|
1446
|
|
|
x = this, |
|
1447
|
|
|
xc = x['c'], |
|
1448
|
|
|
i = x['s'], |
|
1449
|
|
|
e = x['e'], |
|
1450
|
|
|
half = new BigNumber('0.5'); |
|
1451
|
|
|
|
|
1452
|
|
|
// Negative/NaN/Infinity/zero? |
|
1453
|
|
|
if ( i !== 1 || !xc || !xc[0] ) { |
|
1454
|
|
|
return new BigNumber( !i || i < 0 && ( !xc || xc[0] ) |
|
1455
|
|
|
? NaN |
|
1456
|
|
|
: xc ? x : 1 / 0 ) |
|
1457
|
|
|
} |
|
1458
|
|
|
|
|
1459
|
|
|
// Estimate. |
|
1460
|
|
|
i = Math.sqrt( x['toS']() ); |
|
1461
|
|
|
|
|
1462
|
|
|
// Math.sqrt underflow/overflow? |
|
1463
|
|
|
// Pass x to Math.sqrt as integer, then adjust the exponent of the result. |
|
1464
|
|
|
if ( i == 0 || i == 1 / 0 ) { |
|
1465
|
|
|
estimate = xc.join(''); |
|
1466
|
|
|
|
|
1467
|
|
|
if ( !( estimate.length + e & 1 ) ) { |
|
1468
|
|
|
estimate += '0' |
|
1469
|
|
|
} |
|
1470
|
|
|
|
|
1471
|
|
|
r = new BigNumber( Math.sqrt(estimate).toString() ); |
|
1472
|
|
|
r['e'] = ( ( ( e + 1 ) / 2 ) | 0 ) - ( e < 0 || e & 1 ) |
|
1473
|
|
|
} else { |
|
1474
|
|
|
r = new BigNumber( i.toString() ) |
|
1475
|
|
|
} |
|
1476
|
|
|
|
|
1477
|
|
|
i = r['e'] + ( DECIMAL_PLACES += 4 ); |
|
1478
|
|
|
|
|
1479
|
|
|
// Newton-Raphson loop. |
|
1480
|
|
|
do { |
|
1481
|
|
|
approx = r; |
|
1482
|
|
|
r = half['times']( approx['plus']( x['div'](approx) ) ) |
|
1483
|
|
|
} while ( approx['c'].slice( 0, i ).join('') !== |
|
1484
|
|
|
r['c'].slice( 0, i ).join('') ); |
|
1485
|
|
|
|
|
1486
|
|
|
rnd( r, DECIMAL_PLACES -= 4, 10 ); |
|
1487
|
|
|
|
|
1488
|
|
|
return r |
|
1489
|
|
|
}; |
|
1490
|
|
|
|
|
1491
|
|
|
|
|
1492
|
|
|
/* |
|
1493
|
|
|
* n * 0 = 0 |
|
1494
|
|
|
* n * N = N |
|
1495
|
|
|
* n * I = I |
|
1496
|
|
|
* 0 * n = 0 |
|
1497
|
|
|
* 0 * 0 = 0 |
|
1498
|
|
|
* 0 * N = N |
|
1499
|
|
|
* 0 * I = N |
|
1500
|
|
|
* N * n = N |
|
1501
|
|
|
* N * 0 = N |
|
1502
|
|
|
* N * N = N |
|
1503
|
|
|
* N * I = N |
|
1504
|
|
|
* I * n = I |
|
1505
|
|
|
* I * 0 = N |
|
1506
|
|
|
* I * N = N |
|
1507
|
|
|
* I * I = I |
|
1508
|
|
|
* |
|
1509
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber times |
|
1510
|
|
|
* the value of BigNumber(y, b). |
|
1511
|
|
|
*/ |
|
1512
|
|
|
P['times'] = function ( y, b ) { |
|
1513
|
|
|
var c, |
|
1514
|
|
|
x = this, |
|
1515
|
|
|
xc = x['c'], |
|
1516
|
|
|
yc = ( id = 11, y = new BigNumber( y, b ) )['c'], |
|
1517
|
|
|
i = x['e'], |
|
1518
|
|
|
j = y['e'], |
|
1519
|
|
|
a = x['s']; |
|
1520
|
|
|
|
|
1521
|
|
|
y['s'] = a == ( b = y['s'] ) ? 1 : -1; |
|
1522
|
|
|
|
|
1523
|
|
|
// Either NaN/Infinity/0? |
|
1524
|
|
|
if ( !i && ( !xc || !xc[0] ) || !j && ( !yc || !yc[0] ) ) { |
|
1525
|
|
|
|
|
1526
|
|
|
// Either NaN? |
|
1527
|
|
|
return new BigNumber( !a || !b || |
|
1528
|
|
|
|
|
1529
|
|
|
// x is 0 and y is Infinity or y is 0 and x is Infinity? |
|
1530
|
|
|
xc && !xc[0] && !yc || yc && !yc[0] && !xc |
|
1531
|
|
|
|
|
1532
|
|
|
// Return NaN. |
|
1533
|
|
|
? NaN |
|
1534
|
|
|
|
|
1535
|
|
|
// Either Infinity? |
|
1536
|
|
|
: !xc || !yc |
|
1537
|
|
|
|
|
1538
|
|
|
// Return +-Infinity. |
|
1539
|
|
|
? y['s'] / 0 |
|
1540
|
|
|
|
|
1541
|
|
|
// x or y is 0. Return +-0. |
|
1542
|
|
|
: y['s'] * 0 ) |
|
1543
|
|
|
} |
|
1544
|
|
|
y['e'] = i + j; |
|
1545
|
|
|
|
|
1546
|
|
|
if ( ( a = xc.length ) < ( b = yc.length ) ) { |
|
1547
|
|
|
c = xc, xc = yc, yc = c, j = a, a = b, b = j |
|
1548
|
|
|
} |
|
1549
|
|
|
|
|
1550
|
|
|
for ( j = a + b, c = []; j--; c.push(0) ) { |
|
1551
|
|
|
} |
|
1552
|
|
|
|
|
1553
|
|
|
// Multiply! |
|
1554
|
|
|
for ( i = b - 1; i > -1; i-- ) { |
|
1555
|
|
|
|
|
1556
|
|
|
for ( b = 0, j = a + i; |
|
1557
|
|
|
j > i; |
|
1558
|
|
|
b = c[j] + yc[i] * xc[j - i - 1] + b, |
|
1559
|
|
|
c[j--] = b % 10 | 0, |
|
1560
|
|
|
b = b / 10 | 0 ) { |
|
1561
|
|
|
} |
|
1562
|
|
|
|
|
1563
|
|
|
if ( b ) { |
|
1564
|
|
|
c[j] = ( c[j] + b ) % 10 |
|
1565
|
|
|
} |
|
1566
|
|
|
} |
|
1567
|
|
|
|
|
1568
|
|
|
b && ++y['e']; |
|
1569
|
|
|
|
|
1570
|
|
|
// Remove any leading zero. |
|
1571
|
|
|
!c[0] && c.shift(); |
|
1572
|
|
|
|
|
1573
|
|
|
// Remove trailing zeros. |
|
1574
|
|
|
for ( j = c.length; !c[--j]; c.pop() ) { |
|
1575
|
|
|
} |
|
1576
|
|
|
|
|
1577
|
|
|
// No zero check needed as only x * 0 == 0 etc. |
|
1578
|
|
|
|
|
1579
|
|
|
// Overflow? |
|
1580
|
|
|
y['c'] = y['e'] > MAX_EXP |
|
1581
|
|
|
|
|
1582
|
|
|
// Infinity. |
|
1583
|
|
|
? ( y['e'] = null ) |
|
1584
|
|
|
|
|
1585
|
|
|
// Underflow? |
|
1586
|
|
|
: y['e'] < MIN_EXP |
|
1587
|
|
|
|
|
1588
|
|
|
// Zero. |
|
1589
|
|
|
? [ y['e'] = 0 ] |
|
1590
|
|
|
|
|
1591
|
|
|
// Neither. |
|
1592
|
|
|
: c; |
|
1593
|
|
|
|
|
1594
|
|
|
return y |
|
1595
|
|
|
}; |
|
1596
|
|
|
|
|
1597
|
|
|
|
|
1598
|
|
|
/* |
|
1599
|
|
|
* Return a string representing the value of this BigNumber in exponential |
|
1600
|
|
|
* notation to dp fixed decimal places and rounded using ROUNDING_MODE if |
|
1601
|
|
|
* necessary. |
|
1602
|
|
|
* |
|
1603
|
|
|
* [dp] {number} Integer, 0 to MAX inclusive. |
|
1604
|
|
|
*/ |
|
1605
|
|
|
P['toExponential'] = P['toE'] = function ( dp ) { |
|
1606
|
|
|
|
|
1607
|
|
|
return format( this, |
|
1608
|
|
|
( dp == null || ( ( outOfRange = dp < 0 || dp > MAX ) || |
|
1609
|
|
|
|
|
1610
|
|
|
/* |
|
1611
|
|
|
* Include '&& dp !== 0' because with Opera -0 == parseFloat(-0) is |
|
1612
|
|
|
* false, despite -0 == parseFloat('-0') && 0 == -0 being true. |
|
1613
|
|
|
*/ |
|
1614
|
|
|
parse(dp) != dp && dp !== 0 ) && |
|
1615
|
|
|
|
|
1616
|
|
|
// 'toE() decimal places not an integer: {dp}' |
|
1617
|
|
|
// 'toE() decimal places out of range: {dp}' |
|
1618
|
|
|
!ifExceptionsThrow( dp, 'decimal places', 'toE' ) ) && this['c'] |
|
1619
|
|
|
? this['c'].length - 1 |
|
1620
|
|
|
: dp | 0, 1 ) |
|
1621
|
|
|
}; |
|
1622
|
|
|
|
|
1623
|
|
|
|
|
1624
|
|
|
/* |
|
1625
|
|
|
* Return a string representing the value of this BigNumber in normal |
|
1626
|
|
|
* notation to dp fixed decimal places and rounded using ROUNDING_MODE if |
|
1627
|
|
|
* necessary. |
|
1628
|
|
|
* |
|
1629
|
|
|
* Note: as with Javascript's number type, (-0).toFixed(0) is '0', |
|
1630
|
|
|
* but e.g. (-0.00001).toFixed(0) is '-0'. |
|
1631
|
|
|
* |
|
1632
|
|
|
* [dp] {number} Integer, 0 to MAX inclusive. |
|
1633
|
|
|
*/ |
|
1634
|
|
|
P['toFixed'] = P['toF'] = function ( dp ) { |
|
1635
|
|
|
var n, str, d, |
|
1636
|
|
|
x = this; |
|
1637
|
|
|
|
|
1638
|
|
|
if ( !( dp == null || ( ( outOfRange = dp < 0 || dp > MAX ) || |
|
1639
|
|
|
parse(dp) != dp && dp !== 0 ) && |
|
1640
|
|
|
|
|
1641
|
|
|
// 'toF() decimal places not an integer: {dp}' |
|
1642
|
|
|
// 'toF() decimal places out of range: {dp}' |
|
1643
|
|
|
!ifExceptionsThrow( dp, 'decimal places', 'toF' ) ) ) { |
|
1644
|
|
|
d = x['e'] + ( dp | 0 ) |
|
1645
|
|
|
} |
|
1646
|
|
|
|
|
1647
|
|
|
n = TO_EXP_NEG, dp = TO_EXP_POS; |
|
1648
|
|
|
TO_EXP_NEG = -( TO_EXP_POS = 1 / 0 ); |
|
1649
|
|
|
|
|
1650
|
|
|
// Note: str is initially undefined. |
|
1651
|
|
|
if ( d == str ) { |
|
1652
|
|
|
str = x['toS']() |
|
1653
|
|
|
} else { |
|
1654
|
|
|
str = format( x, d ); |
|
1655
|
|
|
|
|
1656
|
|
|
// (-0).toFixed() is '0', but (-0.1).toFixed() is '-0'. |
|
1657
|
|
|
// (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'. |
|
1658
|
|
|
if ( x['s'] < 0 && x['c'] ) { |
|
1659
|
|
|
|
|
1660
|
|
|
// As e.g. -0 toFixed(3), will wrongly be returned as -0.000 from toString. |
|
1661
|
|
|
if ( !x['c'][0] ) { |
|
1662
|
|
|
str = str.replace(/^-/, '') |
|
1663
|
|
|
|
|
1664
|
|
|
// As e.g. -0.5 if rounded to -0 will cause toString to omit the minus sign. |
|
1665
|
|
|
} else if ( str.indexOf('-') < 0 ) { |
|
1666
|
|
|
str = '-' + str |
|
1667
|
|
|
} |
|
1668
|
|
|
} |
|
1669
|
|
|
} |
|
1670
|
|
|
TO_EXP_NEG = n, TO_EXP_POS = dp; |
|
1671
|
|
|
|
|
1672
|
|
|
return str |
|
1673
|
|
|
}; |
|
1674
|
|
|
|
|
1675
|
|
|
|
|
1676
|
|
|
/* |
|
1677
|
|
|
* Return a string array representing the value of this BigNumber as a |
|
1678
|
|
|
* simple fraction with an integer numerator and an integer denominator. |
|
1679
|
|
|
* The denominator will be a positive non-zero value less than or equal to |
|
1680
|
|
|
* the specified maximum denominator. If a maximum denominator is not |
|
1681
|
|
|
* specified, the denominator will be the lowest value necessary to |
|
1682
|
|
|
* represent the number exactly. |
|
1683
|
|
|
* |
|
1684
|
|
|
* [maxD] {number|string|BigNumber} Integer >= 1 and < Infinity. |
|
1685
|
|
|
*/ |
|
1686
|
|
|
P['toFraction'] = P['toFr'] = function ( maxD ) { |
|
1687
|
|
|
var q, frac, n0, d0, d2, n, e, |
|
1688
|
|
|
n1 = d0 = new BigNumber(ONE), |
|
1689
|
|
|
d1 = n0 = new BigNumber('0'), |
|
1690
|
|
|
x = this, |
|
1691
|
|
|
xc = x['c'], |
|
1692
|
|
|
exp = MAX_EXP, |
|
1693
|
|
|
dp = DECIMAL_PLACES, |
|
1694
|
|
|
rm = ROUNDING_MODE, |
|
1695
|
|
|
d = new BigNumber(ONE); |
|
1696
|
|
|
|
|
1697
|
|
|
// NaN, Infinity. |
|
1698
|
|
|
if ( !xc ) { |
|
1699
|
|
|
return x['toS']() |
|
1700
|
|
|
} |
|
1701
|
|
|
|
|
1702
|
|
|
e = d['e'] = xc.length - x['e'] - 1; |
|
1703
|
|
|
|
|
1704
|
|
|
// If max denominator is undefined or null... |
|
1705
|
|
|
if ( maxD == null || |
|
1706
|
|
|
|
|
1707
|
|
|
// or NaN... |
|
1708
|
|
|
( !( id = 12, n = new BigNumber(maxD) )['s'] || |
|
1709
|
|
|
|
|
1710
|
|
|
// or less than 1, or Infinity... |
|
1711
|
|
|
( outOfRange = n['cmp'](n1) < 0 || !n['c'] ) || |
|
1712
|
|
|
|
|
1713
|
|
|
// or not an integer... |
|
1714
|
|
|
( ERRORS && n['e'] < n['c'].length - 1 ) ) && |
|
1715
|
|
|
|
|
1716
|
|
|
// 'toFr() max denominator not an integer: {maxD}' |
|
1717
|
|
|
// 'toFr() max denominator out of range: {maxD}' |
|
1718
|
|
|
!ifExceptionsThrow( maxD, 'max denominator', 'toFr' ) || |
|
1719
|
|
|
|
|
1720
|
|
|
// or greater than the maxD needed to specify the value exactly... |
|
1721
|
|
|
( maxD = n )['cmp'](d) > 0 ) { |
|
1722
|
|
|
|
|
1723
|
|
|
// d is e.g. 10, 100, 1000, 10000... , n1 is 1. |
|
1724
|
|
|
maxD = e > 0 ? d : n1 |
|
1725
|
|
|
} |
|
1726
|
|
|
|
|
1727
|
|
|
MAX_EXP = 1 / 0; |
|
1728
|
|
|
n = new BigNumber( xc.join('') ); |
|
1729
|
|
|
|
|
1730
|
|
|
for ( DECIMAL_PLACES = 0, ROUNDING_MODE = 1; ; ) { |
|
1731
|
|
|
q = n['div'](d); |
|
1732
|
|
|
d2 = d0['plus']( q['times'](d1) ); |
|
1733
|
|
|
|
|
1734
|
|
|
if ( d2['cmp'](maxD) == 1 ) { |
|
1735
|
|
|
break |
|
1736
|
|
|
} |
|
1737
|
|
|
|
|
1738
|
|
|
d0 = d1, d1 = d2; |
|
1739
|
|
|
|
|
1740
|
|
|
n1 = n0['plus']( q['times']( d2 = n1 ) ); |
|
1741
|
|
|
n0 = d2; |
|
1742
|
|
|
|
|
1743
|
|
|
d = n['minus']( q['times']( d2 = d ) ); |
|
1744
|
|
|
n = d2 |
|
1745
|
|
|
} |
|
1746
|
|
|
|
|
1747
|
|
|
d2 = maxD['minus'](d0)['div'](d1); |
|
1748
|
|
|
n0 = n0['plus']( d2['times'](n1) ); |
|
1749
|
|
|
d0 = d0['plus']( d2['times'](d1) ); |
|
1750
|
|
|
|
|
1751
|
|
|
n0['s'] = n1['s'] = x['s']; |
|
1752
|
|
|
|
|
1753
|
|
|
DECIMAL_PLACES = e * 2; |
|
1754
|
|
|
ROUNDING_MODE = rm; |
|
1755
|
|
|
|
|
1756
|
|
|
// Determine which fraction is closer to x, n0 / d0 or n1 / d1? |
|
1757
|
|
|
frac = n1['div'](d1)['minus'](x)['abs']()['cmp']( |
|
1758
|
|
|
n0['div'](d0)['minus'](x)['abs']() ) < 1 |
|
1759
|
|
|
? [ n1['toS'](), d1['toS']() ] |
|
1760
|
|
|
: [ n0['toS'](), d0['toS']() ]; |
|
1761
|
|
|
|
|
1762
|
|
|
return MAX_EXP = exp, DECIMAL_PLACES = dp, frac |
|
1763
|
|
|
}; |
|
1764
|
|
|
|
|
1765
|
|
|
|
|
1766
|
|
|
/* |
|
1767
|
|
|
* Return a string representing the value of this BigNumber to sd significant |
|
1768
|
|
|
* digits and rounded using ROUNDING_MODE if necessary. |
|
1769
|
|
|
* If sd is less than the number of digits necessary to represent the integer |
|
1770
|
|
|
* part of the value in normal notation, then use exponential notation. |
|
1771
|
|
|
* |
|
1772
|
|
|
* sd {number} Integer, 1 to MAX inclusive. |
|
1773
|
|
|
*/ |
|
1774
|
|
|
P['toPrecision'] = P['toP'] = function ( sd ) { |
|
1775
|
|
|
|
|
1776
|
|
|
/* |
|
1777
|
|
|
* ERRORS true: Throw if sd not undefined, null or an integer in range. |
|
1778
|
|
|
* ERRORS false: Ignore sd if not a number or not in range. |
|
1779
|
|
|
* Truncate non-integers. |
|
1780
|
|
|
*/ |
|
1781
|
|
|
return sd == null || ( ( ( outOfRange = sd < 1 || sd > MAX ) || |
|
1782
|
|
|
parse(sd) != sd ) && |
|
1783
|
|
|
|
|
1784
|
|
|
// 'toP() precision not an integer: {sd}' |
|
1785
|
|
|
// 'toP() precision out of range: {sd}' |
|
1786
|
|
|
!ifExceptionsThrow( sd, 'precision', 'toP' ) ) |
|
1787
|
|
|
? this['toS']() |
|
1788
|
|
|
: format( this, --sd | 0, 2 ) |
|
1789
|
|
|
}; |
|
1790
|
|
|
|
|
1791
|
|
|
|
|
1792
|
|
|
/* |
|
1793
|
|
|
* Return a string representing the value of this BigNumber in base b, or |
|
1794
|
|
|
* base 10 if b is omitted. If a base is specified, including base 10, |
|
1795
|
|
|
* round according to DECIMAL_PLACES and ROUNDING_MODE. |
|
1796
|
|
|
* If a base is not specified, and this BigNumber has a positive exponent |
|
1797
|
|
|
* that is equal to or greater than TO_EXP_POS, or a negative exponent equal |
|
1798
|
|
|
* to or less than TO_EXP_NEG, return exponential notation. |
|
1799
|
|
|
* |
|
1800
|
|
|
* [b] {number} Integer, 2 to 36 inclusive. |
|
1801
|
|
|
*/ |
|
1802
|
|
|
P['toString'] = P['toS'] = function ( b ) { |
|
1803
|
|
|
var u, str, strL, |
|
1804
|
|
|
x = this, |
|
1805
|
|
|
xe = x['e']; |
|
1806
|
|
|
|
|
1807
|
|
|
// Infinity or NaN? |
|
1808
|
|
|
if ( xe === null ) { |
|
1809
|
|
|
str = x['s'] ? 'Infinity' : 'NaN' |
|
1810
|
|
|
|
|
1811
|
|
|
// Exponential format? |
|
1812
|
|
|
} else if ( b === u && ( xe <= TO_EXP_NEG || xe >= TO_EXP_POS ) ) { |
|
1813
|
|
|
return format( x, x['c'].length - 1, 1 ) |
|
1814
|
|
|
} else { |
|
1815
|
|
|
str = x['c'].join(''); |
|
1816
|
|
|
|
|
1817
|
|
|
// Negative exponent? |
|
1818
|
|
|
if ( xe < 0 ) { |
|
1819
|
|
|
|
|
1820
|
|
|
// Prepend zeros. |
|
1821
|
|
|
for ( ; ++xe; str = '0' + str ) { |
|
1822
|
|
|
} |
|
1823
|
|
|
str = '0.' + str |
|
1824
|
|
|
|
|
1825
|
|
|
// Positive exponent? |
|
1826
|
|
|
} else if ( strL = str.length, xe > 0 ) { |
|
1827
|
|
|
|
|
1828
|
|
|
if ( ++xe > strL ) { |
|
1829
|
|
|
|
|
1830
|
|
|
// Append zeros. |
|
1831
|
|
|
for ( xe -= strL; xe-- ; str += '0' ) { |
|
1832
|
|
|
} |
|
1833
|
|
|
} else if ( xe < strL ) { |
|
1834
|
|
|
str = str.slice( 0, xe ) + '.' + str.slice(xe) |
|
1835
|
|
|
} |
|
1836
|
|
|
|
|
1837
|
|
|
// Exponent zero. |
|
1838
|
|
|
} else { |
|
1839
|
|
|
if ( u = str.charAt(0), strL > 1 ) { |
|
1840
|
|
|
str = u + '.' + str.slice(1) |
|
1841
|
|
|
|
|
1842
|
|
|
// Avoid '-0' |
|
1843
|
|
|
} else if ( u == '0' ) { |
|
1844
|
|
|
return u |
|
1845
|
|
|
} |
|
1846
|
|
|
} |
|
1847
|
|
|
|
|
1848
|
|
|
if ( b != null ) { |
|
1849
|
|
|
|
|
1850
|
|
|
if ( !( outOfRange = !( b >= 2 && b <= 36) ) && |
|
1851
|
|
|
( b == (b | 0) || !ERRORS ) ) { |
|
1852
|
|
|
str = convert( str, b | 0, 10, x['s'] ); |
|
1853
|
|
|
|
|
1854
|
|
|
// Avoid '-0' |
|
1855
|
|
|
if ( str == '0') { |
|
1856
|
|
|
return str |
|
1857
|
|
|
} |
|
1858
|
|
|
} else { |
|
1859
|
|
|
|
|
1860
|
|
|
// 'toS() base not an integer: {b}' |
|
1861
|
|
|
// 'toS() base out of range: {b}' |
|
1862
|
|
|
ifExceptionsThrow( b, 'base', 'toS' ) |
|
1863
|
|
|
} |
|
1864
|
|
|
} |
|
1865
|
|
|
|
|
1866
|
|
|
} |
|
1867
|
|
|
|
|
1868
|
|
|
return x['s'] < 0 ? '-' + str : str |
|
1869
|
|
|
}; |
|
1870
|
|
|
|
|
1871
|
|
|
|
|
1872
|
|
|
/* |
|
1873
|
|
|
* Return as toString, but do not accept a base argument. |
|
1874
|
|
|
*/ |
|
1875
|
|
|
P['valueOf'] = function () { |
|
1876
|
|
|
return this['toS']() |
|
1877
|
|
|
}; |
|
1878
|
|
|
|
|
1879
|
|
|
|
|
1880
|
|
|
// Add aliases for BigDecimal methods. |
|
1881
|
|
|
//P['add'] = P['plus']; |
|
1882
|
|
|
//P['subtract'] = P['minus']; |
|
1883
|
|
|
//P['multiply'] = P['times']; |
|
1884
|
|
|
//P['divide'] = P['div']; |
|
1885
|
|
|
//P['remainder'] = P['mod']; |
|
1886
|
|
|
//P['compareTo'] = P['cmp']; |
|
1887
|
|
|
//P['negate'] = P['neg']; |
|
1888
|
|
|
|
|
1889
|
|
|
|
|
1890
|
|
|
// EXPORT |
|
1891
|
|
|
|
|
1892
|
|
|
|
|
1893
|
|
|
// Node and other CommonJS-like environments that support module.exports. |
|
1894
|
|
|
if ( typeof module !== 'undefined' && module.exports ) { |
|
1895
|
|
|
module.exports = BigNumber |
|
1896
|
|
|
|
|
1897
|
|
|
//AMD. |
|
1898
|
|
|
} else if ( typeof define == 'function' && define.amd ) { |
|
1899
|
|
|
define( function () { |
|
1900
|
|
|
return BigNumber |
|
1901
|
|
|
}) |
|
1902
|
|
|
|
|
1903
|
|
|
//Browser. |
|
1904
|
|
|
} else { |
|
1905
|
|
|
global['BigNumber'] = BigNumber |
|
1906
|
|
|
} |
|
1907
|
|
|
|
|
1908
|
|
|
})( this ); |
|
1909
|
|
|
|
|
1910
|
|
|
|